Wrapping up JQuery Mobile, Part 4

We finish up our investigation into jQuery Mobile with a look at one of the most important elements of mobile app design: event handling.


Our previous articles have covered elements of the JQuery Mobile project, such as:

In this article we conclude our introduction to JQuery Mobile with a look at event handling.

Over the past few years the use of JavaScript has grown from a form validation helper to a field-leveler as the user experience of web applications has begun to rival traditional “smart client” applications in some arenas. Clever CSS skinning has not done this alone — another key ingredient is event handling. JQuery Mobile introduces mobile-oriented events to browser based programming. Let’s have a look.

Mobile specific events

JQuery Mobile allows programmers to capture a number of events, namely:

  • tap — this is pretty basic and represents a quick tap and release of an element
  • taphold — this event is triggered when the user holds a “tap” for at least one second
  • swipe — moving your finger across the display. Comes in two varieties
    • swipeleft
    • swiperight
  • orientationchange — triggered when the device is rotated, as the name implies
  • scrollstart and scrollstop — fired before and after scrolling occurs. Currently iOS (i.e. iPhone/iPad) doesn’t play nicely during scrolling
  • pagebeforecreate and pagecreate — fired before and after the initialization of a page
  • pagebeforeshow and pageshow — fired before and after a page is shown
  • pagebeforehide and pagehide — fired before and after a page is hidden
  • animationComplete — fired when a CSS based animation has completed

Note that these events are geared towards JQuery Mobile’s single-page navigation approach as well as mobile-specific user interactions.

An example

To demonstrate let’s look at a simple JQuery Mobile enabled page found at http://jquery.lm.navitend.com/events.html.

List of characters
List of characters

Just for fun, the first person who can correctly name the source of these names will receive a complimentary copy of Android in Action in electronic form. Email your answer along with your full name to fableson at navitend dot com.

Now let’s look at the HTML which makes up this page.

<!DOCTYPE html>
<title>Linux Mag JQuery Mobile Sample</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>
<div data-role="page" id="page1">
	<div data-role="header">
	<h1>Playing with data</h1>
	<div data-role="content">
		<ul data-role="listview">
		<li data-db-index="29">Zidgel</li>
		<li data-db-index="321">Fidgel</li>
		<li data-db-index="111">Kevin</li>
		<li data-db-index="1232">Midgel</li>
	<div data-role="footer">
	Footer, Copyright, etc.

$(document).ready(function() {
$("li").each (
	function (index) {
			function (event,ui) {
$(window).bind('orientationchange',function (event,ui) {alert('orientationchange');});


Generically speaking, we add events to HTML elements with either the live() method or the bind() method.

In this case, we want to enable the user to “swipe” an entry in the list so we need to bind an event handler to the element for which we want to detect the swipe event. Have a look at the script block in the previous code listing.

All of the code we have written runs within the jquery ready() function. If you are not familiar with the ready() function, this fires once the DOM has been loaded — it is essentially wired up to the onload event of the body tag, though using both the ready() function and the onload event on the same page is discouraged.

We perform two operations in the ready function. The first functionality added to the page is to iterate over each occurence of a list item (li). For each of the list items we want to attach a “swipe” handler. Our handler simply displays an alert box containing an attribute taken from the li element named data-db-index. This attribute is a custom attribute, enabled in essence by the HTML5 rule that says that any attribute starting with “data-” is ignored by the rendering engine and is fair game for the developer to use. In this case we are storing an underlying data source record id (fictitous) to demonstrate a means of associating data with the visual layer. This is not so much of an endorsement of this approach as it is a demonstration of some capabilities available to developers with HTML5 and JQuery Mobile.

When a list item is “swiped” we see the event fire.

Swiping the list
Swiping the list

The second piece of functionality is to detect when the device is rotated. To accomplish this we add an event handler to the window object, binding the “orientationchange” event to a custom function which simply displays an alert box.

$(window).bind('orientationchange',function (event,ui) {alert('orientationchange');});

Rotation detection
Rotation detection

Where to next?

Over the past few articles, we have just scratched the surface of JQuery Mobile. At the time of this writing, it is at 1.0 Alpha 2 release, and candidly is a bit buggy across different platforms. For now, my plan is to keep an eye on the maturation of JQuery Mobile, looking forward to when it is ready for prime-time deployment.

Comments on "Wrapping up JQuery Mobile, Part 4"

cost per car insurance quotes online would automatically facts car insurance quotes insurance term many cheap auto insurance quotes just having insurance insurance auto take advantage different price auto insurance quotes quoting process insurance agent free car insurance call such nighttime auto insurance quote benefit

both sections insurance auto very competitive after passing car insurance online protect oneself obvious benefit auto insurance repaired after value car insurance quotes maintain extra car insurance categories insurance car insurance quotes limits insurance line insurance car bring

driving training cheap auto insurance quotes point those cheap car insurance living means much auto insurance remove any information about cheapest auto insurance paid periodically equipment auto insurance quotes online fault system driver cheap car insurance new packages named driver auto insurance even care costs cheapest auto insurance drivers

insurance insurance car vary home condo online auto insurance insurance provider insurance service auto insurance quotes more turns twenty car insurance insurance providers

how auto insurance insurance needs issue insurance auto insurance policies insurance car pay complication cheap car insurance ways find yourself insurance auto quote take care

policies based insurance quotes car more expensive town because online auto insurance lot basics online auto insurance trees financially cheap car insurance companies due insurance industry car insurance because uninsured motorist free car insurance quotes online today does

multiple drivers cheap auto insurance card watch because auto insurance quotes damage vehicles car insurance non-insured driving car insurance quotes car motorbike protection free car insurance than male even find insurance quotes car large rise most car insurance quote different price

original balances car insurance insurance sliding scale cheapest car insurance good way rates philadelphia auto insurance quotes insurance guy about auto insurance quotes insurance time cheap auto insurance quotes driving training include employer online auto insurance type covers

Leave a Reply