简体   繁体   中英

onload not working after i use $.mobile.changePage

I will change page by $.mobile.changePage with code

 $.mobile.changePage("profile.html", { transition : "slide"},false);

topage profile.html code

<html>
<head>
<script type="text/javascript">

function func()
{
document.getElementById("d").innerHTML="String"
}
</script>
</head>
 <body onload="func()">
<div id="d"></div>
</body>
</html>

javascript code

function func()
{
document.getElementById("d").innerHTML="String"
}

not work ,How to resolve?

If you take a look at the documentation , the reloadPage option is initially false which means that the page will not be reloaded, hence the onload event of the current document won't be fired again.

To make your body onload event to work again, you will need to add this option and set its value to true :

var options={
    transition: "slide",
    reloadPage: true
};
$.mobile.changePage("profile.html", options, false);

jQuery Mobile uses Ajax to load pages. When first page is loaded, all tags and codes inside <html> are loaded. However, when you change page or load another html file, it loads only first page div data-role=page neglecting all tags and js outisde that div.

If you have any extra JS code, place it inside page div data-role="page" .

<div data-role="page">
  <script>
    document.getElementById("d").innerHTML="String"
  </script>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM