Introducing jQuery Mobile
jQuery Mobile (JQM) bills itself as a "touch-optimized Web framework for smartphones and tablets." It reads special HTML attributes that you put into your webpages and adds code to reformat the page. You end up with a sophisticated mobile Web app that has the cross-platform benefits of HTML5 and that closely mimics the polished look and feel of native mobile apps.
Visit the Supplementary Material section for a list of apps that use jQuery Mobile.
JQM currently works on almost every popular smartphone, tablet device browser, and operating system in use today, including:
- iOS devices (iPhone, iPod touch, iPad)
- Android
- Windows Phone
- BlackBerry
- Kindle 3 and Kindle Fire
- Firefox
The great thing about jQuery Mobile is that just by using it, your app becomes optimized for all these platforms!
Let's see and try out a simple jQuery Mobile example app.
First, follow these steps to create a basic JQM template that you can reuse for your future JQM projects.
-
Open Komodo Edit, and select File > New > New File.
-
Copy the following code, and paste it into your blank file.
-
Save this document as jqmtemplate.html, and then preview it in your browser. You should see something like this:
Your basic JQM template
If you have access to a mobile device and a space on the Web where you can upload and access this file, check it out on your mobile device. For instructions on how to get a free Web hosting account, see the FAQs in Lesson 2. Here's what it looks like on my phone:
JQM template on an Android smartphone
I hope that you can already see the potential power and time savings that a framework such as jQuery Mobile can bring you. Just wait until you see what comes next!
Before I show you the real power of jQuery Mobile, let's examine the structure of this template.
Check out the code that makes up this page. Look first at the <link> element and the two <script> elements in the <head> of the document. In order for JQM to function, every HTML document in your Web app has to have these three elements, which import, or "include," three files into the document. These are (in order):
- The jQuery Mobile style sheet: This contains all the code to make JQM apps look good.
- The jQuery library: JQM uses this basic system to interact with the page.
- The jQuery Mobile framework: This is what enables the unique features of JQM to style your mobile app and create native app-like buttons and toolbars.
Inside the body of the document, you use the data-role attribute to tell JQM the role of different sections of markup within your app. The first data-role, "page," is a container that you use to create a single page of your mobile app. It's actually possible to have multiple JQM pages in one HTML document, as you'll see later.
Inside the div with the data-role value of "page," you divide your content into three categories: header, content, and footer.
If you use only three standard JQM-included files and these four data-role values, you've already made your app far more cross-platform capable than any native app could ever be. And it took you only a few lines of code!
Creating and Linking Pages
JQM lets you create multiple pages within a single HTML document by stacking <div> elements with the data-role attributes set to "page." As long as you give each of these pages a unique ID attribute, you can link between them by using a standard HTML link and a hash tag (#) followed by the ID value.
For example, here's a JQM app with two linked pages:
If you save and preview this app in a browser, you'll get a nice surprise: JQM automatically creates a native-like transition effect when you click between the two pages. You should see one page slide out of view while the one you're linking to slides into view. Very slick!
Enabling the Toolbars
jQuery Mobile has two toolbars: header and footer. You can enable them by using the data-role values of "header" and "footer" (ingeniously enough).
The header should be the first element within a page, and the footer should be the last. The header can contain up to two buttons, and it usually also contains a page title. On native iOS apps, the header typically contains the "Back" and "Home" buttons as well as the title.
Here's some sample code for putting two buttons in the header of the sample JQM app you just built:
Go back to the basic JQM template you set up earlier in this chapter. Replace the header elements in both pages of the previous sample JQM app to see the header in action and to try it out. (If you have trouble with this, give me a holler in the Discussion Area.)
When I test it on iOS, it looks like this. The header can contain two links and a title.
iOS version
Looking good!
There's one thing that bothers me about this app so far, though. I want the footer to be at the bottom of the screen. So please insert data-position="fixed" into the beginning tag of the footer div, like this:
When you refresh the page in your browser, you'll see the footer toolbar fixed at the bottom of the window:
App with a fixed footer
Now you know the basics of creating JQM pages, linking between them, and creating toolbars. Please continue on to Chapter 4, where we'll discuss about some more capabilities of this mobile framework.