简体   繁体   中英

mobile.changePage() not working in function

I made a simple code to create a "Splash screen".

I'm using localstorage for my data. If I load it for the first time, I get data from URL and I can change page using $.mobile.changePage() . But if localstorage data already stored, changing page code doesn't work.

function HOME() {
    if (window.localStorage.getItem('newhome')) {
        $.mobile.changePage("#pageone", {
            transition: "none",
            changeHash: false
        });
    } else {
        $.get('http:/someurl', function (data) { //comment this line if we make apk
            window.localStorage.setItem('newhome', JSON.stringify(data));
        }).done(function (data) {
            $.mobile.changePage("#pageone", {
                transition: "none",
                changeHash: false
            });
        });
    }
}
<body>
  <div id="splash" data-role="page"><h1>splash here</h1></div>
  <div id="pageone" data-role="page"><p>my content here</p></div>
</body>

If I run different functions eg console.log , they work, but not $.mobile.changePage .

The reason your page doesn't change is because it never enters the if loop. The else is executed yes, but the method .getItem() does not return a Boolean type. to check that your key is actually set you should check against null. So your code would look a bit more like this:

if (localStorage.getItem("username") === null) {
$.get('http://www.thejewishinsights.com/wp/wp-json/posts', function (data) { //comment this line if we make apk
        window.localStorage.setItem('newhome', JSON.stringify(data));
    }).done(function (data) {
        $.mobile.changePage("#pageone", {
            transition: "none",
            changeHash: false
        });
    });
}
else{
$.mobile.changePage("#pageone", {
        transition: "none",
        changeHash: false
    });
}

I didn't check the code but you should get the idea. Also i would advise against .changePage as it will be removed from jQuery mobile 1.5 as stated in their documentation http://api.jquerymobile.com/jQuery.mobile.changePage/

I hope This helps. Cheers and happy new year!

Have you tried using this:?

$(":mobile-pagecontainer").pagecontainer("change","#newpage");

I think that JQMobile Official Site recommend using this instead of the one you use.

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