Why javascript is not working after changePage

When I changed the page, 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 is not executed when changing the page; can someone explain why this is so and how to fix it?

+3
source share
1 answer

This is because the page with the change will cause the pages to reload, and JQM will not get the head of the newly loaded page in the DOM, so no scripts <head>will be included in the document.

Therefore, you can put all your JS into an external file and include it on every HTML page of the site that you need.

You can also put your JavaScript code inside an element data-role="page"and you enable JS.

+1
source

All Articles