By Paul McFedries

It’s safe to say that jQuery makes web development faster, easier, and even more pleasurable. Now let’s talk briefly about yet another jQuery library: jQuery Mobile, which offers widgets optimized for mobile web apps.

What is jQuery Mobile?

Most folks nowadays have a mobile device of some description, which means that most people are used to doing our digital duties using mobile interfaces. These interfaces include standard mobile elements such as fixed headers and footers, navigation bars, list views, tabs, switches that turn on and off, and hidden menus invoked by a “hamburger” icon.

Coding elements such as these is possible, but it would be a ton of work. Fortunately, you can skip all of that because the hardcore geeks at jQuery Mobile have done it all for you. jQuery Mobile is a set of mobile-optimized widgets that make it easy for you to design your mobile web app to have the look and feel of a native mobile app.

Best of all, the jQuery Mobile components work just like the jQuery UI widgets. Now all you have to do is incorporate jQuery Mobile into your app.

Adding jQuery Mobile to your web app

jQuery Mobile consists of two files:

  • A JavaScript (.js) file that you add to your page by using a <script> tag with a reference to the external script file.
  • A CSS (.css) file that you add to your page by using a <link rel="stylesheet"> tag with a reference to the external CSS file.

How do you get these files? You have three ways to go about it:

  • Download the files and use the default jQuery Mobile styles. In this case, surf to jquerymobile.com/download and click the ZIP File link. The file you get will have a name like jquery.mobile-1.x.y.zip, where x and y denote the current version. Decompress the ZIP file and then copy the jquery.mobile-1.x.y.min.js and jquery.mobile-1.x.y.min.css files to your mobile web app’s JavaScript and CSS folders. Then set up your <link> and <script> tags to reference the files:
<link rel="stylesheet" href="/css/jquery.mobile-1.x.y.min.css">
<script src="/js/jquery.mobile-1.x.y.min.js"></script>

(Remember to replace x and y with the actual version numbers of your downloaded file.)

  • Use custom jQuery Mobile styles. In this case, surf to themeroller.jquerymobile.com and use the ThemeRoller app to set your custom colors, fonts, and other styles. Click the Download Theme ZIP File button, type a theme name, and then click Download ZIP. Decompress the downloaded ZIP file, copy the CSS file from the Themes folder, and then add it to your mobile web app’s CSS folder. Then set up your <link> tag to reference the file:
<link rel="stylesheet" href="/css/custom.min.css">

Replace custom with the custom theme name you provided. Note that this only gives you the CSS for jQuery Mobile. You still need to download the jQuery Mobile JavaScript file.

  • Link to a remote version of the file. Several content delivery networks (CDNs) store the jQuery Mobile files and let you link to them. Here are the tags to use for Google’s CDN:
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.x.y/jquery.mobile.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.x.y/jquery.mobile.min.js"></script>

Again, in both cases, be sure to replace x and y with the actual version numbers of the latest version of jQuery Mobile.

You also need to add jQuery to your page. However, currently, jQuery Mobile is only compatible with version 2 of jQuery, so be sure to link to that version, not version 3.