Events

Home

jQuery Mobile offers several custom events that build upon native events to create useful hooks for development. Note that these events employ various touch, mouse, and window events, depending on event existence, so you can bind to them for use in both handheld and desktop environments. You can bind to these events like you would with other jQuery events, using live() or bind().

Important: Use pageInit(), not $(document).ready()

The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pageinit event. This event is explained in detail at the bottom of this page.

Important: pageCreate() vs pageInit()

Prior to Beta 2 the recommendation to users wishing to manipulate jQuery Mobile enhanced page and child widget markup was to bind to the pagecreate event. In Beta 2 an internal change was made to decouple each of the widgets by binding to the pagecreate event in place of direct calls to the widget methods. As a result, users binding to the pagecreate in mobileinit would find their binding executing before the markup had been enhanced by each of the plugins. In keeping with the lifecycle of the jQuery UI Widget Factory, the initialization method is invoked after the create method, so the pageinit event provides the correct timing for post enhancement manipulation of the DOM and/or Javascript objects. In short, if you were previously using pagecreate to manipulate the enhanced markup before the page was shown its very likely you'll want to migrate to 'pageinit'.

Touch events

tap
Triggers after a quick, complete touch event.
taphold
Triggers after a held complete touch event (close to one second).
swipe

Triggers when a horizontal drag of 30px or more (and less than 20px vertically) occurs within 1 second duration but these can be configured:

  • scrollSupressionThreshold (default: 10px) – More than this horizontal displacement, and we will suppress scrolling
  • durationThreshold (default: 1000ms) – More time than this, and it isn’t a swipe
  • horizontalDistanceThreshold (default: 30px) – Swipe horizontal displacement must be more than this.
  • verticalDistanceThreshold (default: 75px) – Swipe vertical displacement must be less than this.
swipeleft
Triggers when a swipe event occurred moving in the left direction.
swiperight
Triggers when a swipe event occurred moving in the right direction.

Orientation change event

orientationchange
Triggers when a device orientation changes (by turning it vertically or horizontally). When bound to this event, your callback function can leverage a second argument, which contains an orientation property equal to either "portrait" or "landscape". These values are also added as classes to the HTML element, allowing you to leverage them in your CSS selectors. Note that we currently bind to the resize event when orientationChange is not natively supported.

Scroll events

scrollstart
Triggers when a scroll begins. Note that iOS devices freeze DOM manipulation during scroll, queuing them to apply when the scroll finishes. We're currently investigating ways to allow DOM manipulations to apply before a scroll starts.
scrollstop
Triggers when a scroll finishes.

Page Loading Events

Whenever an external page is loaded into the application 2 events are fired. The first is pagebeforeload. The 2nd event will be either pageload or pageloadfailed.

pagebeforeload

Triggered before any load request is made. Callbacks bound to this event can call preventDefault() on the event to indicate that they are handling the load request. Callbacks that do this *MUST* make sure they call resolve() or reject() on the deferred object reference contained in the data object passed to the callback.

The data object, passed as the 2nd arg to the callback function contains the following properties:

  • url (string)
    • The absolute or relative URL that was passed into $.mobile.loadPage() by the caller.
  • absUrl (string)
    • The absolute version of the url. If url was relative, it is resolved against the url used to load the current active page.
  • dataUrl (string)
    • The filtered version of absUrl to be used when identifying the page and updating the browser location when the page is made active.
  • deferred (object)
    • Callbacks that call preventDefault() on the event, *MUST* call resolve() or reject() on this object so that changePage() requests resume processing. Deferred object observers expect the deferred object to be resolved like this:

      
      $( document ).bind( "pagebeforeload", function( event, data ){
      
      	// Let the framework know we're going to handle the load.
      
      	event.preventDefault();
      
      	// ... load the document then insert it into the DOM ...
      	// at some point, either in this callback, or through
      	// some other async means, call resolve, passing in
      	// the following args, plus a jQuery collection object
      	// containing the DOM element for the page.
      
      	data.deferred.resolve( data.absUrl, data.options, page );
      
      });

      or rejected like this:

      
      $( document ).bind( "pagebeforeload", function( event, data ){
      
      	// Let the framework know we're going to handle the load.
      
      	event.preventDefault();
      
      	// ... load the document then insert it into the DOM ...
      	// at some point, if the load fails, either in this
      	// callback, or through some other async means, call
      	// reject like this:
      
      	data.deferred.reject( data.absUrl, data.options );
      
      });
  • options (object)
    • This object contains the options that were passed into $.mobile.loadPage().
pageload
Triggered after the page is successfully loaded and inserted into the DOM. Callbacks bound to this event will be passed a data object as its 2nd arg. This object contains the following information:
  • url (string)
    • The absolute or relative URL that was passed into $.mobile.loadPage() by the caller.
  • absUrl (string)
    • The absolute version of the url. If url was relative, it is resolved against the url used to load the current active page.
  • dataUrl (string)
    • The filtered version of absUrl to be used when identifying the page and updating the browser location when the page is made active.
  • options (object)
    • This object contains the options that were passed into $.mobile.loadPage().
pageloadfailed
Triggered if the page load request failed. By default, after dispatching this event, the framework will display a page failed message and call reject() on the deferred object contained within the event's data object. Callbacks can prevent this default behavior from executing by calling preventDefault() on the event.

The data object, passed as the 2nd arg to the callback function contains the following properties:

  • url (string)
    • The absolute or relative URL that was passed into $.mobile.loadPage() by the caller.
  • absUrl (string)
    • The absolute version of the url. If url was relative, it is resolved against the url used to load the current active page.
  • dataUrl (string)
    • The filtered version of absUrl to be used when identifying the page and updating the browser location when the page is made active.
  • deferred (object)
    • Callbacks that call preventDefault() on the event, *MUST* call resolve() or reject() on this object so that changePage() requests resume processing. Deferred object observers expect the deferred object to be resolved like this:

      
      $( document ).bind( "pageloadfailed", function( event, data ){
      
      	// Let the framework know we're going to handle things.
      
      	event.preventDefault();
      
      	// ... attempt to load some other page ...
      	// at some point, either in this callback, or through
      	// some other async means, call resolve, passing in
      	// the following args, plus a jQuery collection object
      	// containing the DOM element for the page.
      
      	data.deferred.resolve( data.absUrl, data.options, page );
      
      });

      or rejected like this:

      
      $( document ).bind( "pageloadfailed", function( event, data ){
      
      	// Let the framework know we're going to handle things.
      
      	event.preventDefault();
      
      	// ... attempt to load some other page ...
      	// at some point, if the load fails, either in this
      	// callback, or through some other async means, call
      	// reject like this:
      
      	data.deferred.reject( data.absUrl, data.options );
      
      });
  • options (object)
    • This object contains the options that were passed into $.mobile.loadPage().

Page show/hide events

Whenever a page is shown or hidden in jQuery Mobile, two events are triggered on that page. The events triggered depend on whether that page is being shown or hidden, so when a page transition occurs, there are actually 4 events triggered: 2 for each page.

pagebeforeshow
Triggered on the page being shown, before its transition begins.
pagebeforehide
Triggered on the page being hidden, before its transition begins.
pageshow
Triggered on the page being shown, after its transition completes.
pagehide
Triggered on the page being hidden, after its transition completes.

Note that all four of these events expose a reference to either the next page (nextPage) or previous page (prevPage), depending on whether the page is being shown or hidden, and whether that next or previous page exists (the first ever page shown does not have a previous page to reference, but an empty jQuery object is provided just the same). You can access this reference via the second argument of a bound callback function. For example:


$( 'div' ).live( 'pageshow',function(event, ui){
  alert( 'This page was just hidden: '+ ui.prevPage);
});

$( 'div' ).live( 'pagehide',function(event, ui){
  alert( 'This page was just shown: '+ ui.nextPage);
});

Also, for these handlers to be invoked during the initial page load, you must bind them before jQuery Mobile executes. This can be done in the mobileinit handler, as described on the global config page.

Page initialization events

Internally, jQuery Mobile auto-initializes plugins based on the markup conventions found in a given "page". For example, an input element with a type of range will automatically generate a custom slider control.

This auto-initialization is controlled by the "page" plugin, which dispatches events before and after it executes, allowing you to manipulate a page either pre-or-post initialization, or even provide your own intialization behavior and prevent the auto-initializations from occuring. Note that these events will only fire once per "page", as opposed to the show/hide events, which fire every time a page is shown and hidden.

pagebeforecreate

Triggered on the page being initialized, before most plugin auto-initialization occurs.


$( '#aboutPage' ).live( 'pagebeforecreate',function(event){
  alert( 'This page was just inserted into the dom!' );
});

Note that by binding to pagebeforecreate, you can manipulate markup before jQuery Mobile's default widgets are auto-initialized. For example, say you want to add data-attributes via JavaScript instead of in the HTML source, this is the event you'd use.


$( '#aboutPage' ).live( 'pagebeforecreate',function(event){
  // manipulate this page before its widgets are auto-initialized
});
pagecreate

Triggered when the page has been created in the DOM (via ajax or other) but before all widgets have had an opportunity to enhance the contained markup. This event is most useful for user's wishing to create their own custom widgets for child markup enhancement as the jquery mobile widgets do.


$( '#aboutPage' ).live( 'pagecreate',function(event){
  ( ":jqmData(role='sweet-plugin')" ).sweetPlugin();
});
pageinit

Triggered on the page being initialized, after initialization occurs. We recommend binding to this event instead of DOM ready() because this will work regardless of whether the page is loaded directly or if the content is pulled into another page as part of the Ajax navigation system.


$( '#aboutPage' ).live( 'pageinit',function(event){
  alert( 'This page was just enhanced by jQuery Mobile!' );
});

Virtual mouse events

We provide a set of "virtual" click events that normalize mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. This still retains the order of event firing in the traditional mouse environment, should multiple handlers be registered on the same element for different events.

vmouseover
Normalized event for handling touch or mouse mouseover events
vmousedown
Normalized event for handling touch or mouse mousedown events
vmousemove
Normalized event for handling touch or mouse mousemove events
vmouseup
Normalized event for handling touch or mouse mouseup events
vclick
Normalized event for handling touch or mouse click events
vmousecancel
Normalized event for handling touch or mouse mousecancel events

Animation Events

jQuery Mobile exposes the animationComplete plugin, which you can utilize after adding or removing a class that applies a CSS transition.