Tono Nam
Tono Nam

Reputation: 36048

Jquery Mobile makes script execute twice

why does the test message shows twice on:

<!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 from http://jquerymobile.com/demos/1.2.0/docs/about/getting-started.html and as I started doing some examples I noticed that my javascript methods where being called twice.

Here is the code in JsFiddle:

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

Upvotes: 4

Views: 1429

Answers (2)

Mattias Andersson
Mattias Andersson

Reputation: 190

You can use

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

.. just fine, as long as you put it outside of the div with data-role="page". No script tags allowed inside the page markup.

Upvotes: 0

peterm
peterm

Reputation: 92785

As Explosion Pills perfectly stated you have to play by 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");
});

use

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

jsFiddle is here

Upvotes: 2

Related Questions