YouTip LogoYouTip

Jquerymobile Events Page

jQuery Mobile Page Events body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; line-height: 1.6; color: #333; margin: 0; padding: 0; background-color: #f4f4f4; } .container { max-width: 1200px; margin: 0 auto; padding: 20px; } header { background-color: #2c3e50; color: white; padding: 1rem 0; text-align: center; } header h1 { margin: 0; } nav { background-color: #34495e; padding: 0.5rem; } nav ul { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; justify-content: center; } nav li { margin: 0 10px; } nav a { color: white; text-decoration: none; font-size: 0.9rem; } nav a:hover { text-decoration: underline; } .sidebar { background-color: #ecf0f1; padding: 1rem; margin-bottom: 20px; border-radius: 5px; } .sidebar h3 { margin-top: 0; border-bottom: 2px solid #bdc3c7; padding-bottom: 0.5rem; } .sidebar ul { list-style: none; padding-left: 0; } .sidebar li { margin-bottom: 0.5rem; } .sidebar a { color: #2980b9; text-decoration: none; } .sidebar a:hover { text-decoration: underline; } .content { background-color: white; padding: 2rem; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } h2 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 0.5rem; } h3 { color: #34495e; } table { width: 100%; border-collapse: collapse; margin: 1rem 0; } th, td { border: 1px solid #ddd; padding: 12px; text-align: left; } th { background-color: #3498db; color: white; } tr:nth-child(even) { background-color: #f2f2f2; } pre { background-color: #f8f8f8; padding: 1rem; border-radius: 5px; overflow-x: auto; border: 1px solid #ddd; } code { font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; } .try-it { display: inline-block; background-color: #27ae60; color: white; padding: 0.5rem 1rem; text-decoration: none; border-radius: 5px; margin-top: 1rem; } .try-it:hover { background-color: #2ecc71; } hr { border: 0; border-top: 1px solid #eee; margin: 2rem 0; } .note { background-color: #fff3cd; border-left: 4px solid #ffc107; padding: 1rem; margin: 1rem 0; } footer { text-align: center; padding: 1rem; margin-top: 2rem; color: #7f8c8d; font-size: 0.9rem; }

jQuery Mobile Page Events

jQuery Mobile Page Events

In jQuery Mobile, events related to working with pages 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 to load.
  • Page Transition - Before and after a page transition.
  • Page Change - When a page is changed, or fails to change.

For complete information on all jQuery Mobile events, please visit our jQuery Mobile Events Reference.


jQuery Mobile Initialization Events

When a typical page in jQuery Mobile is initialized, it goes through three phases:

  • Before page creation
  • Page creation
  • Page initialization

The events triggered at each phase can be used to insert or manipulate code.

Event 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 the 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 (on success) or pageloadfailed (on failure).

The table below explains these events:

Event 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 Triggered if a page load request fails. By default, an "Error Loading Page" message is displayed.

The following demonstrates how the pageload and pageloadfailed 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 the next.

A page transition involves two pages: a "from" page and a "to" page - these transitions make the change from the current active page (the "from" page) to the new page (the "to" page) more dynamic.

Event Description
pagebeforeshow Triggered on the "to" page, before the transition animation starts.
pageshow Triggered on the "to" page, after the transition animation completes.
pagebeforehide Triggered on the "from" page, before the transition animation starts.
pagehide Triggered on the "from" page, after the transition animation completes.

The following demonstrates how the 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("Now showing page two");
});

$(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("Now hiding page two");
});
Try it

Β© 2024 Tutorial. All rights reserved.

← Firebug TutorialEv Onmouseup β†’