Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the Javascript not working after changePage

When I changed the page, the javascript did not execute.

index.html

$("#login").submit(function(e){
e.preventDefault(); 
$.mobile.changePage('main.html', {
    reverse : false,
    changeHash : false,
    allowSamePageTransition : true,
    reloadPage:true,
    transition: "flow"
});
});

main.html

<script type="text/javascript">
    $(document).ready(function () {
        alert("Test");
    });
</script>

The above javascript in main.html does not execute upon page change; can anyone explain why this is and how to fix it?

like image 392
Eric Lo Avatar asked Mar 28 '26 21:03

Eric Lo


1 Answers

It's because change page will cause reload of pages and JQM is not getting the head section of recently loaded page in the DOM so any scripts in the <head> of the document will not be included.

Hence, you can put all of your JS into an external file and include it on each HTML page of the site that you required.

You can also place your JavaScript code inside the data-role="page" element and you JS will be included.

like image 153
Felix Avatar answered Mar 31 '26 04:03

Felix