You are binding to the window.load
event which only fires on the initial page load. In jQuery Mobile, each time you navigate to another page it is brought into the DOM via AJAX, so no full-page-refresh occurs. That is why you need to bind to a page-event
using .delegate()
. page-events
are triggered on the specific data-role="page"
elements that it relates to. For example, if you have a page with an id of some_id
you can bind to the pagecreate
event for that page by doing this:
$(document).delegate('#some_id', 'pagecreate', function(){
alert('Alright, it worked!');
});
If you want to bind an event to all pseudo-pages in your site then this selector will work:
$(document).delegate('[data-role="page"]', 'pagecreate', function(){
var $page = $(this);
$page.find('.container').find('img').each(function(){
var $image = $(this),
imgClass = ($image.width()/$image.height() > 1) ? 'wide' : 'tall';
$image.addClass(imgClass);
})
});
This block of code does not need to be placed in a document.ready
or window.load
event handler, it can just be included globally.
Check-out this documentation on jQuery Mobile events: http://jquerymobile.com/demos/1.0/docs/api/events.html
To learn more about $.mobile
check-out this page and the different functions you can use: http://jquerymobile.com/demos/1.0/docs/api/methods.html
.page()
has beed depreciated in favor of trigger('create')
. This is how you initialize any jQuery Mobile widget.