jQuery Mobile Page Events
jQuery Mobile Page Events
In jQuery Mobile, the events dealing with pages are categorized into four types:
Page Initialization - Before the page is created, when the page is created, and after the page is initialized
Page Load/Unload - When an external page is loaded, unloaded, or encounters a failure
Page Transition - Before and after the page transition
Page Change - When the page is changed, or encounters a failure
For complete information on all jQuery Mobile events, please visit our jQuery Mobile Events Reference Manual.
jQuery Mobile Initialization Events
When a typical page in jQuery Mobile is initialized, it goes through three phases:
Before the page is created
Page creation
Page initialization
Events triggered in each phase can be used to insert or manipulate code.
Event | Description |
---|---|
pagebeforecreate | Triggered when the page is about to be initialized and before jQuery Mobile has started enhancing the page. |
pagecreate | Triggered when the page has been created but before enhancement is complete. |
pageinit | Triggered when the page has been initialized and after jQuery Mobile has completed page enhancement. |
The following example demonstrates when each event is triggered during the creation of a page in jQuery Mobile:
Example
$(document).on("pagebeforecreate", function(event){
alert("pagebeforecreate event triggered!");
});
$(document).on("pagecreate", function(event){
alert("pagecreate event triggered!");
});
jQuery Mobile Load Events
Page load events pertain to external pages.
Two events are triggered whenever an external page is loaded into the DOM. The first is pagebeforeload, and the second is pageload (success) or pageloadfailed (failure).
These events are explained in the table below:
Event | Description |
---|---|
pagebeforeload | Triggered before any page load request is made. |
pageload | Triggered after the page has been successfully loaded and inserted into the DOM. |
pageloadfailed | Triggered if the page load request fails. By default, an "Error Loading Page" message is displayed. |
The following demonstrates how the pageload and pageloadfailed events work:
Example
jQuery Mobile Transition Events
We can also use events during the transition from one page to the next.
Page transitions involve two pages: a "coming" page and a "going" page - these transitions make the change from the current active page ("coming" page) to the new page ("going" page) more dynamic.
Event | Description |
---|---|
pagebeforeshow | Triggered on the "going" page, before the transition animation starts. |
pageshow | Triggered on the "going" page, after the transition animation completes. |
pagebeforehide | Triggered on the "coming" page, before the transition animation starts. |
pagehide | Triggered on the "coming" page, after the transition animation completes. |
The following demonstrates how transition events work: