JQuery Mobile executes script execution twice

why the test message is displayed twice:

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script> </head> <body> <script type="text/javascript"> alert("Test"); </script> </body> </html> 

I copied the headers from http://jquerymobile.com/demos/1.2.0/docs/about/getting-started.html , and when I started to do some examples, I noticed that my javascript methods called twice.

Here is the code in JsFiddle:

http://jsfiddle.net/LsxBW/2/

+4
source share
2 answers

As explosion-proof pistols have perfectly stated , you need to play according to jQM rules . At least:

 <div data-role="page" id="page1"> <div data-role="header"> <h1>Page Title</h1> </div><!-- /header --> <div data-role="content"> <p>Page content goes here.</p> </div><!-- /content --> <div data-role="footer"> <h4>Page Footer</h4> </div><!-- /footer --> </div><!-- /page --> 
 $(function() { alert("Test"); }); 

using

 $(document).on("pageinit", "#page1", function(){ alert("Test"); }); 

jsFiddle here

+2
source

you can use

 $(function() { alert("Test"); }); 

.. just fine if you expose it outside the div with data-role = "page". The script tag is allowed inside the page layout.

0
source

All Articles