jQuery Mobile Quick Start

by on September 24, 2012 12:57 pm

jQuery Mobile is a HTML5-based UI system built on the jQuery Javascript library and jQuery UI. It is lightweight, flexible, and easy to use with all mobile device platforms. 

Depending on which particular report you read, you probably have less than two years before mobile visits will outnumber typical desktop visits. Can your website meet the demands of your mobile visitors by next year? Does your website have the capabilities you need to begin working with a mobile website today?  Let’s take a look at the basics of jQuery Mobile so that you can be ready.

Index Page:

I’m not one to put together an example that doesn’t really do anything, so let’s start with a simplified index page that I put together for the Ozarktrail.com website as a demo: http://Ozarktrail.com/mobile.

Do keep in mind that what you put on your mobile site will not be quite the same as what displays on your desktop website, but I will not go into that today.


<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
Ozark Trail - Home
      		<link href="themes/theme.min.css" rel="stylesheet" />
      		<link href="http://code.jquery.com/mobile/1.1.1/jquery.mobile.structure-1.1.1.min.css" rel="stylesheet" /><script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script><script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script></pre>
<div data-role="page" data-theme="a">
<div data-role="header" data-position="inline">
<h1>The Ozark Trail</h1>
</div>
<div data-role="content" data-theme="a">
<ul data-role="listview" data-inset="true" data-filter="true">
	<li><a href="trail.php">Trail Sections</a></li>
	<li><a href="maps.php">Detailed Maps</a></li>
	<li><a href="conditions.php">Trail Conditions</a></li>
</ul>
</div>
</div>
<pre>

In the <head> section, a meta “viewport” tag sets the screen width to the pixel width of the device. I used ThemeRoller for Mobile to build a custom mobile-themed stylesheet.  And script tags for jQuery and jQuery Mobile references are added (jQuery Mobile 1.1 works with both 1.6.4 and 1.7.1 versions of jQuery core).

In the body, a <div> with data-role=”page” is used to define the page, and the header bar (data-role=”header”) and content region (data-role=”content”) are added inside to create a basic page (these are both optional). These data- attributes are HTML5 attributes and used throughout jQuery Mobile to turn basic markup into an “enhanced and styled widget”.

Next I added a listview within the data-role=”content” <div>. jQuery Mobile includes many options for listviews  and you can add a dynamic search filter using data-filter=”true”.

Here is a screen-shot of the first page:

Ozark Trail

 

Details Page:

Next let’s look at a simplified version of a details page (conditions.php), which has some dynamic content:

<!--?php    require_once('dbconn.php');    mysql_select_db($database_name, $link_identifier);    $query = "select * from trailconditions ";    $results = mysql_query($query, $link_identifier) or die(mysql_error());    ?-->

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
Ozark Trail - Trail Conditions
      	<link href="themes/ot-theme.min.css" rel="stylesheet" />
      	<link href="http://code.jquery.com/mobile/1.1.1/jquery.mobile.structure-1.1.1.min.css" rel="stylesheet" /><script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script><script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>

</pre>
<div data-role="page" data-theme="a">
<div data-role="header" data-position="inline">
<h1>Trail Conditions</h1>
</div>
<div data-role="content" data-theme="a">
<a href="index.php" data-role="button" data-icon="home">Home</a>
<div style="color: #ecd487;">
 <!--?php while ($row = mysql_fetch_assoc($results)) { ?-->
<strong><!--?php echo $row['trailsection']; ?--> - <!--?php echo $row['trailstatus']; ?--></strong>

 <!--?php echo $row['conditiondesc']; ?-->
</div>
</div>
</div>
<pre>

Starting with the template from the index.php, add some simple php to connect to your database and get some data.

In the body section data-role=”content” <div> I added a Home button data-role=”button” to link back to the index.php page, and added a basic loop to display the trail section, status, and trail conditions description.

I have kept this example as simplified as possible to concentrate on the jQuery Mobile aspects, but I would suggest a Model-View-Controller (MVC) approach so the model and controller for your current website can be reused with a new mobile view page. A nice two part example for using PHP, MySQL, and jQuery Mobile with the MVC pattern can be found at tutorialzine.com.
Part 1 link: http://tutorialzine.com/2011/08/jquery-mobile-product-website/
Part 2 link: http://tutorialzine.com/2011/08/jquery-mobile-mvc-website-part-2/

Here is a screen-shot of the second page:

Ozark Trail 2

 

Hopefully this gives you just a hint of what jQuery Mobile has to offer and what can be done with just a little effort.  Check out jquerymobile.com for much, much more and take a look at jquerymobile.com/demos for documentation, demos, and to get started.

— Todd Horn, asktheteam@keyholesoftware.com

  • Share:

Leave a Reply

Things Twitter is Talking About
  • We are under construction! Knocking down walls & expanding our offices in Leawood, KS this week. http://t.co/ph6JElh8lr
    September 23, 2014 at 9:46 AM
  • Did you know? Today in 1986 it was ruled that computer code is protected under copyright law - http://t.co/mCmWPvKOBE
    September 22, 2014 at 4:30 PM
  • Don't miss Alok Pandey's very first post on the Keyhole blog - Creating a UI Designer for Quick Development http://t.co/dWYMCOO3rf
    September 22, 2014 at 2:43 PM
  • If you're a Java dev, you're likely familiar with Annotations. But have you created your own #Java Annotations? Ex - http://t.co/mf1F3eIDY3
    September 21, 2014 at 5:15 PM
  • Check out a quick intro to Functional Reactive Programing and #JavaScript - http://t.co/4LSt6aPJvG
    September 20, 2014 at 11:15 AM
  • In Part 2 of our series on creating your own #Java annotations, learn about processing them with the Reflection API - http://t.co/E1lr3RmjI7
    September 19, 2014 at 12:15 PM
  • The life of a Keyhole consultant - A Delicate Balance: It’s What We Do http://t.co/ToRpWY3aix Blog as true today as the day it was written.
    September 19, 2014 at 9:50 AM
  • 7 Things You Can Do to Become a Better Developer - http://t.co/llPNMUN8nQ
    September 19, 2014 at 8:43 AM
  • .@jessitron Good luck, you'll do great! Our team really enjoyed your KCDC14 talks.
    September 18, 2014 at 10:19 AM
  • RT @woodwardjd: 7 deadly sins of programming. I think I did all of this last week. #strangeloop http://t.co/f7QFq1SpqW
    September 18, 2014 at 10:03 AM
  • In Part 2 of our series on creating your own #Java annotations, learn about processing them with the Reflection API - http://t.co/E1lr3RmjI7
    September 17, 2014 at 3:18 PM
  • We send out our free monthly tech newsletter tomorrow - dev tips/articles via email. Not on the list? Sign up: http://t.co/h8kpjn419s
    September 16, 2014 at 2:58 PM
  • Want to chuckle? If programming languages were vehicles -http://t.co/quqHsUFCtR #funny
    September 16, 2014 at 11:41 AM
  • In Part 2 of our series on creating your own annotations, learn about processing #Java annotations using Reflection: http://t.co/DJZvQuarkc
    September 16, 2014 at 9:06 AM
  • Don't miss @jhackett01's newest post on the Keyhole blog - Processing #Java Annotations Using Reflection: http://t.co/E1lr3RmjI7
    September 15, 2014 at 12:02 PM
  • We're pretty excited - Keyhole's #BikeMS team raised 158% of their fundraising goal to benefit @MidAmericaMS. Plus, they had a great ride!
    September 15, 2014 at 10:38 AM
  • A huge welcome to David Kelly (@rheomatic) who officially joins the Keyhole team today! :-)
    September 15, 2014 at 10:00 AM
  • Sending warm thoughts to @eastlack, @cdesalvo, @wdpitt & all participating in #BikeMS this AM. Thanks for helping in the fight against MS!
    September 13, 2014 at 8:10 AM
  • .@rheomatic We are so excited to have you joining the team! Welcome :-)
    September 12, 2014 at 4:11 PM
  • As the official holiday is a Saturday, we're celebrating today! Happy (early) #ProgrammersDay to you! http://t.co/1CvUfrzytE
    September 12, 2014 at 1:55 PM
Keyhole Software
8900 State Line Road, Suite 455
Leawood, KS 66206
ph: 877-521-7769
© 2014 Keyhole Software, LLC. All rights reserved.