1

I'm quite new in html5 & js and i have some troubles to develop a jQuery Mobile exemple.

From this, i'm just calling another header (partial) when a product is selected to render.

_header2.php:

<!DOCTYPE html>
<head> 
<title>h2: <?php echo $_GET['product']; ?> </title>
<meta charset="UTF-8" />
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;' name='viewport' />
<link rel='stylesheet' href='assets/css/styles_mob.css'/>
<link rel='stylesheet' href='assets/css/styles.css' />
<link href='assets/css/jquery-mobile.css?<?php echo filemtime("assets/css/jquery-mobile.css");?>' type='text/css' rel='stylesheet' />
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>
<script type='text/javascript' src='http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js'></script>
</script>

<script type='text/javascript'>
function mymessage(msg)
{
if(!msg) msg="?";
alert(msg);
}

</script>

</head>

<body onload="mymessage('onload !')">


<div data-role="page" id="Home"> 
<div data-role="header" data-theme="b">
<a href="./" data-icon="home" data-iconpos="notext" data-transition="fade">Home</a>
<h1> <?php echo $title?></h1>
</div>

<div data-role="content">   

Header code appair correctly on the generated page but js call of mymessage() in body tag doesn't work. Same problem when i try to call it from another partial code (_product.php) :

<li><a href='#Gallery1' onClick='mymessage(\"press gallery\")' >...</li>

...concole return: referenceError: mymessage is not defined

Everything rock only when a refreshing the current page !!

I've rode some posts (1, 2, 3) with similar problem but i'm still lost.

Any idea please ?

Community
  • 1
  • 1
spin0
  • 21
  • 4

2 Answers2

1

You're missing the <html> tag. You also have an extra </script> closing tag. This could potentially lead to your error.

Grampa
  • 1,623
  • 10
  • 25
0

There are all sorts of strange coding styles and errors like missing the <html> tag, an extra </script> closing tag, missing the closing tag on your anchor in <li><a href='#Gallery1' onClick='mymessage(\"press gallery\")' >...</li>, and list items should be inside <ul></ul>.

Try this code, it should work. Then analyze it and learn!

<!DOCTYPE html>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">

<title>h2: <?php echo $_GET['product'];?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;">

<link rel="stylesheet" type="text/css" href="assets/css/styles_mob.css">
<link rel="stylesheet" type="text/css" href="assets/css/styles.css">
<!-- this next line makes me wonder.. why?!  -->
<link rel="stylesheet" type="text/css" href="assets/css/jquery-mobile.css?<?php echo filemtime('assets/css/jquery-mobile.css');?>">

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>

<script type="text/javascript">//<![CDATA[ 
function mymessage(msg){
    msg=msg||'?'; //the usual way to provide defaults
    alert(msg);
}

window.onload=function(){ //setting your onload event.
    mymessage('onload !');
};
//]]>  
</script>


</head><body>

<div data-role="page" id="Home"> 
<div data-role="header" data-theme="b">
<a href="./" data-icon="home" data-iconpos="notext" data-transition="fade">Home</a>
<h1><?php echo $title;?></h1>
</div>

<div data-role="content">  

<ul> <!-- Here is your link, working -->
    <li><a href="#Gallery1" onclick="mymessage('press gallery');">...</a></li>
</ul>

<!-- added some closing tags for demo-sake -->
</div></div>
</body></html>

PS: since all the beginner errors and the fact that this is just part of your code, I strongly assume the rest of your code has such strange error's to.

Good Luck!!


UPDATE:
Depending on what you'd like to accomplish on page-load, jQuery Mobile might completely change the ballpark.
As can be read in jQuery Mobile's documentation:

By default all navigation within jQuery Mobile is based on changes and updates to location.hash. Whenever possible, page changes will use a smooth transition between the current "page" and the next, whether it is either already present in the DOM, or is automatically loaded via Ajax.

Another quote from the documentation:

Use $(document).bind('pageinit'), not $(document).ready()

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event.

This means that the regular techniques described will only fire when you first visit the page, not while navigating the page ajax-style.

Thus in jQuery mobile you must use pageinit or pageshow or one of the other events that are explained in the documentation that suit your exact purpose.

Example how to get pageinit fire for every page (tested live on your site):

$(document).on('pageinit','[data-role=page]', function(){
    mymessage('hihi');    
});

Or alternatively you could turn off ajax in jQuery Mobile, like this (depends on version):

$(document).bind("mobileinit", function(){
    $.mobile.ajaxEnabled = false;
});
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • Thank's for corrections @GitaarLAB,
      tags shouldn't missing, please look at the code of this [full page](http://archidavy.net/html5_store/?category=3.1&product=3.1.1), i didn't mentionned other piece of code (photoswipe) but it should not matter...
    – spin0 Aug 22 '12 at 01:45
  • You are welcome! So I see you already implemented my corrections to your code and your original problem (`mymessage(msg)` not firing) is now fixed. At least.. I see the alert: 'onload !` when I visited [your link](http://archidavy.net/html5_store/?category=3.1&product=3.1.1). Just don't forget to clear your body onload attribute now! I also found a HUGE error in your code: NEVER EVER make 'lines' in comments with `--` like ``. Instead DO: ``!!! There is not enough space here to _fully_ explain this. Just trust me on this one! Don't forget to accept an answer eventually! – GitaarLAB Aug 22 '12 at 02:07
  • Hi @GitaarLAB, i've implemented your corrections :) but still have the same firing problem if we follow the navigation steps from the begining : 1) clean brother cache, 2) click home button (top left) to start from category menu, 3) select 'Archives' category, 4) select 1st item on right, 5) at this point we should see the alert firing before displaying the selected item as single product, isn't it ?.. – spin0 Aug 22 '12 at 06:38
  • First: why did you add `$('body').removeAttr('onload');`? When this line is executed (in head), there is no body yet, so that line will give a error. Isn't it easier to change `` to ``? Second: I don't see anything on the right to the 'archives' category (so I can't click on the 1st item to the right). Lastly, I think you are using ajax to update parts of you site. It's absolutely normal that your body/window onload will not fire on ajax-loads since the body and window where loaded when you opened the page. Add call to mymessage to ajax load-handler. – GitaarLAB Aug 22 '12 at 14:28
  • I forgot a navigation step between 3) and 4), please @GitaarLAB after u press 'Archives(3), select also 'ERP(3.1) wich is a sub category, then u will see the list on right. May be i'm blind but i didn't see any Ajax on [mentionned source](http://tutorialzine.com/2011/08/jquery-mobile-product-website/), only jQuery Mobile. I didn't finish to search about removing attribute... thank's :) – spin0 Aug 22 '12 at 17:25
  • So there is your problem then, you are using Ajax without realizing it. Please read the update to my answer that explains why this happens and how to fix it! – GitaarLAB Aug 22 '12 at 18:54
  • Yeah @GitaarLAB ! thank's a lot, i didn't rode enough the documentation :) So now i have to take care of ajax, and $(document).bind(..) make js calls work all the time, exept at the begining, for the 1st selected product. – spin0 Aug 23 '12 at 01:28
  • Thank's again ! Just a last thing, **jquery.mobile.subpage** plugin is the solution to integrate code using multipage...may be it's not related to this but would you have an idea about product's link "gallery x.x.x", above 'Alert' (new[page](http://archidavy.net/html5_store/?category=3.1&product=3.1.1)) ) only run when reset page, and after display old gallery. It must call a page (id='Gallery1') generated for selected product. I try to remove old class instances on load, but console return 0. – spin0 Aug 23 '12 at 03:07
  • Nice find on the subpage plugin! As to your other question: if you can't get an answer via the documentation, then I really feel you should open a new question for this issue: that would also draw proper and _fresh_ attention to your current problem at hand AND keep SO neatly sorted (SO is your 'new' knowledge-base to!!). Please don't be offended (I have a hard time saying 'no' in my life). Good luck with your site, I'm still pretty impressed how well you're doing considering you call yourself a 'newbee'. – GitaarLAB Aug 23 '12 at 03:34