0

I can get the pagecontainer change function to work inside a function that is called with a button click. But for some reason the pagecontainer change function isn't working when I run the code outside of a function. I do a check to see if a user is logged in and then want to do a page change/redirect if they aren't logged in.

 <script>
//works
    function test() {
        $(':mobile-pagecontainer').pagecontainer('change', '#event-list-page');
    }
//doesn't work
    var user = window.localStorage.getItem("user");
    if (user == null){
        alert('not logged in. Should redirect.');
        $(':mobile-pagecontainer').pagecontainer('change', '#event-list-page');
    }
 </script>

What is the best solution for this problem? Should I just do a javascript redirect? Or do I need to wait for the page to load all the way or something?

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131

2 Answers2

2

You should intiate the $.mobile.pageconatiner first, example:

$.mobile.pageContainer.pagecontainer({ defaults: true });
Denis C de Azevedo
  • 6,276
  • 2
  • 32
  • 49
MMSA
  • 810
  • 8
  • 22
0

I think that the jQuery mobile has to have the entire page loaded first because it does a bunch of funny things pre-loading pages and doing ajax transitions and things. It's not a simple redirect like setting window.location. If you pt it in a document.ready block it will work.

$( document ).ready(function() {
                    $.mobile.pageContainer.pagecontainer('change', '#event-list-page');
                    });
Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
  • You shouldn't use `.ready` in JQM. You need to execute code on `pagebeforechange`. – Omar Jan 03 '14 at 21:03
  • I'm reading around trying to find some examples on using pagebeforechange, and am not finding a lot of success. Can you post an example? – Chase Roberts Jan 03 '14 at 21:21