jQuery Mobile Page Events
jQuery Mobile Page Events
Events that deal with pages in jQuery Mobile are divided into four categories:
- Page Initialization - before a page is created, when it is created, and after it is initialized
- Page Load/Unload - when an external page is loaded, unloaded, or fails
- Page Transition - before and after a page transition
- Page Change - when a page is changed, or fails
For complete information about all jQuery Mobile events, visit our jQuery Mobile Events Reference Manual。
jQuery Mobile Initialization Events
When a typical page in jQuery Mobile is initialized, it goes through three stages:
- Before page creation
- Page creation
- Page initialization
The events triggered at each stage can be used to insert or manipulate code.
| Events | Description |
|---|---|
| pagebeforecreate | This event is triggered when a page is about to be initialized and before jQuery Mobile has started enhancing the page. |
| pagecreate | This event is triggered when a page has been created, but before enhancement is complete. |
| pageinit | This event is triggered when a page has been initialized and after jQuery Mobile has finished enhancing the page. |
The following example demonstrates when each event is triggered when creating a page in jQuery Mobile:
Example
$(document).on("pagebeforecreate",function(event){
alert("pagebeforecreate event triggered!");
});
$(document).on("pagecreate",function(event){
alert("pagecreate event triggered!");
});
Try it »
jQuery Mobile Load Events
Page load events belong to external pages.
Whenever an external page is loaded into the DOM, two events are triggered. The first is pagebeforeload, and the second is pageload (success) or pageloadfailed (failure).
These events are explained in the table below:
| Events | Description |
|---|---|
| pagebeforeload | Triggered before any page load request is made. |
| pageload | Triggered after a page has been successfully loaded and inserted into the DOM. |
| pageloadfailed | This event is triggered if a page load request fails. By default, the "Error Loading Page" message is displayed. |
The following demonstrates how the pageload and pagloadfailed events work:
Example
$(document).on("pageload",function(event,data){
alert("pageload event triggered!\nURL: " + data.url);
});
$(document).on("pageloadfailed",function(event,data){
alert(";Sorry, the requested page does not exist.");
});
Try it »
jQuery Mobile Transition Events
We can also use events when transitioning from one page to another.
A page transition involves two pages: an "incoming" page and an "outgoing" page - these transitions make the change from the current active page (the "incoming" page) to the new page (the "outgoing" page) more dynamic.
| Events | Description |
|---|---|
| pagebeforeshow | Triggered on the "outgoing" page, before the transition animation starts. |
| pageshow | Triggered on the "outgoing" page, after the transition animation completes. |
| pagebeforehide | Triggered on the "incoming" page, before the transition animation starts. |
| pagehide | Triggered on the "incoming" page, after the transition animation completes. |
The following demonstrates how transition events work:
Example
$(document).on("pagebeforeshow","#pagetwo",function(){ // when entering page two
alert("Page two is about to be shown");
});
$(document).on("pageshow","#pagetwo",function(){ // when entering page two
alert("Page two is now shown");
});
$(document).on("pagebeforehide","#pagetwo",function(){ // when entering page two
alert("Page two is about to be hidden");
});
$(document).on("pagehide","#pagetwo",function(){ // When leaving pagetwo
alert("Page two is now hidden");
});
Try it »