0

Is there a way to change pages in jQuery Mobile but still have $(document).ready function initiate? I seem to running into some problems when calling $.mobile.changePage because my pages are not always fully loaded....

Or instead of finding an alternative to changePage, is there a way to refresh a page once it's been loaded into the DOM by changePage??

Apollo
  • 8,874
  • 32
  • 104
  • 192
  • possible duplicate of [Jquery Mobile - $.mobile.changepage not loading external .JS files](http://stackoverflow.com/questions/7449402/jquery-mobile-mobile-changepage-not-loading-external-js-files) – Jasper Aug 02 '12 at 20:10
  • This is a pretty common question/problem. When you move to an AJAX navigation model, you have to re-think your event bindings since you're working with the same DOM throughout the user's experience. I've answered similar questions and I think this answer could help you: http://stackoverflow.com/questions/7449402/jquery-mobile-mobile-changepage-not-loading-external-js-files/7449731#7449731 – Jasper Aug 02 '12 at 20:10

2 Answers2

2

This answer was wrong and useless, so here's one that will actually help people... - Jasper

Documentation: http://jquerymobile.com/demos/1.1.1/docs/api/methods.html

Specifically you are looking for the reloadPage option:

reloadPage (boolean, default: false) Forces a reload of a page, even if it is already in the DOM of the page container. Used only when the 'to' argument of changePage() is a URL.

So, something like this will work:

$.mobile.changePage('/some/url.html', {
    reloadPage : true
});

You can also bind to the pageinit event for specific pseudo-pages in order to run code specifically for those pseudo-pages:

$(document).delegate('#page-1', 'pageinit', function () {
    //run code for #page-1 pseudo-page
}).delegate('#page-2', 'pageinit', function () {
    //run code for #page-2 pseudo-page
}).delegate('#page-3', 'pageinit', function () {
    //run code for #page-3 pseudo-page
});

Then I would suggest putting all of the code for the whole site in a single .js include and include it in the or outside of any data-role="page" or data-role="dialog" elements of each document. This way the code for your site will always be present, no matter how the user has landed on the site or navigated around.

Jasper
  • 75,717
  • 14
  • 151
  • 146
Apollo
  • 8,874
  • 32
  • 104
  • 192
  • @Jaspar I could create a JSFiddel of the effect but I know from personal experience document.ready is fired after $.mobile.changePage(). – Apollo Aug 02 '12 at 19:48
  • I setup a quick example that shows the events fired when navigating from page to page in a jQuery Mobile website (version 1.1.1): http://apexeleven.com/stackoverflow/document-ready/. You'll notice that the `ready` event is not fired after the initial page-load. – Jasper Aug 02 '12 at 19:57
1

In jQuery Mobile, ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handlers (i.e. all events bounded using $(document).ready()) only execute for the first page. To execute a specific event on a page when it is shown you can try putting this on a specific page,

$(document)
  .unbind("pageshow.somenamespace")
  .bind("pageshow.somenamespace", 
    function() {
      // code here
    }
  );
jay c.
  • 1,521
  • 10
  • 9