Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile - Do something on page load

I want to do something every time a page loads. It's something that fixes the way the mobile site looks on different devices so it needs to happen on AJAX loads too.

At the moment, I've tried the traditional $(function(){ /*...*/ }); approach but that only works on the first load, and not subsequent AJAX loads.

I've been looking for the right event to bind to but I'm failing pretty hard.

like image 435
Oli Avatar asked Jun 23 '11 14:06

Oli


2 Answers

You can use JQuery to bind to each "page" (div's set with the data-role=page attribute) and use the pageshow event (there are some others as well: pagebeforeshow, pagehide, pagebeforehide).

$(document).delegate('.ui-page', 'pageshow', function () {
    //Your code for each page load here
});

http://api.jquerymobile.com/pageshow/

Note: this answer was written for jQuery Mobile 1.0.1, see the documentation link above for more information.

like image 115
Jasper Avatar answered Oct 07 '22 18:10

Jasper


You can listen to the pageshow or pagecreate event and do your work there.

http://jquerymobile.com/test/docs/api/events.html

like image 35
J.T.Sage Avatar answered Oct 07 '22 18:10

J.T.Sage