Jquery Mobile Web Development Essentials-Third Edition - Sample Chapter
Jquery Mobile Web Development Essentials-Third Edition - Sample Chapter
Third Edition
jQuery Mobile is a HTML5-based touch-optimized
web framework. jQuery Mobile can be used to build
responsive cross-platform websites and apps for a
wide range of smartphones, tablets, and desktop
devices. The jQuery Mobile framework can be integrated
with other mobile app frameworks such as PhoneGap,
IBM MobileFirst, and more.
This book explains how to add the framework to
your HTML pages to create rich, mobile-optimized
web pages with minimal effort. You'll learn how to
use jQuery Mobile's automatic enhancements and
congure the framework for customized, powerful,
mobile-friendly websites. We then dig into forms, events,
and styling. You'll see how jQuery Mobile automatically
enhances content, and will nd out how to use the
JavaScript API to build complex sites.
E x p e r i e n c e
D i s t i l l e d
Raymond Camden
P U B L I S H I N G
$ 44.99 US
28.99 UK
Sa
m
pl
C o m m u n i t y
Andy Matthews
Third Edition
ee
Third Edition
Build a powerful and practical jQuery-based framework in order to
create mobile-optimized websites
Raymond Camden
Andy Matthews
Andy Matthews has been working as a software engineer for nearly 20 years with
experience in a wide range of industries and a skillset that includes UI/UX, graphic
design, and programming. He is the coauthor of the books Creating Mobile Apps with
jQuery Mobile and jQuery Mobile Web Development Essentials by Packt Publishing. He
has written for online publications, such as Adobe, NetTuts, and .NET Magazine.
He has spoken at conferences all over the country, and has developed a number of
projects for the open source community.
Preface
What is jQuery Mobile?
On August 11, 2010, nearly six years ago, John Resig (creator of jQuery) announced
the jQuery Mobile project. While it focused on the UI framework, it was also a
recognition of jQuery itself as a tool for mobile websites and that work would be
done to the core framework itself to make it work better on devices. Release after
release, the jQuery Mobile project evolved into a powerful framework, encompassing
more platforms, more features, and better performance with every update.
But what do we mean when we say a UI framework? What does it mean for developers
and designers? jQuery Mobile provides a way to turn regular HTML (and CSS) into
mobile-friendly websites. As you will see soon in the first chapter, you can take a
regular HTML page, add the required bits for jQuery Mobile (essentially, lines of
HTML), and your page is transformed into a mobile-friendly version instantly.
Unlike other frameworks, jQuery Mobile is focused on HTML. In fact, for a
framework tied to jQuery, you can do a heck of a lot of work without writing one
line of JavaScript. It's a powerful, practical way of creating mobile websites that
any existing HTML developer can pick up and adapt within a few hours. Compare
this to other frameworks, such as Sencha Touch. Sencha Touch is also a powerful
framework, but its approach is radically different, using JavaScript to help define
and layout pages. jQuery Mobile is much friendlier to people who are more familiar
with HTML as opposed to JavaScript. jQuery Mobile is touch-friendly, which will
make sense to anyone who has used a smartphone and struggled to click the exact
spot on a website with tiny text and hard-to-spot links. It will make sense to anyone
who has accidentally clicked on a Reset button instead of Submit. jQuery Mobile
will enhance your content to help solve these issues. Regular buttons become large,
fat, and easy-to-hit buttons. Links can be turned into list-based navigation systems.
Content can be split into virtual pages with smooth transitions. You will be surprised
just how jQuery Mobile works without writing much code at all.
Preface
Help!
While we'd like to think that this book will cover every single possible topic you
would need for all your jQuery Mobile needs, most likely there will be things we
can't cover. If you need help, there are a couple of places you can try.
Preface
Preface
Chapter 11, Enhancing jQuery Mobile, demonstrates how to change the default
appearance of your jQuery Mobile websites by selecting and creating unique themes.
Chapter 12, Creating Native Applications, takes what you've learned previously
and shows how to use the open source PhoneGap project to create real
native applications.
Chapter 13, Becoming an Expert Building an RSS Reader Application, expands upon the
previous chapter by creating an application that lets you add and read RSS feeds on
mobile devices.
[1]
As we said, it isn't too complex, right? Let's take a quick look at this in the browser:
[2]
Chapter 1
Not so bad, right? But let's take a look at the same page in a mobile simulator:
[3]
Wow, the text is in a barely readable font size. You've probably seen web pages like
this before on your mobile device. You can, of course, typically use pinch and zoom
or double-click actions to increase the size of the text. But it would be preferable to
have the page render immediately in a mobile-friendly view. This is where jQuery
Mobile enters.
[4]
Chapter 1
An important note: at the time this book was written, jQuery Mobile
was at version 1.4.5. Obviously, by the time you read this book, a
later version may be released. The filenames you see listed in the
previous screenshot are version-specific, so keep in mind that they
may look a bit different for you.
The ZIP file contains demos and both the minified and regular versions of the jQuery
Mobile framework. Additional files are provided for theming and other purposes,
but your main concern will be with jquery.mobile-1.4.5.min.css and jquery.
mobile-1.4.5.min.js. You will typically want to use the minified version in your
production apps though. The images folder contains various images used by jQuery
Mobile's CSS file. Of course, you also need to include the jQuery library.
You can download this separately at http://www.jquery.com.
[5]
This is used to help inform the browser about the type of content it will be
dealing with
2. Add a viewport meta tag:
<meta name="viewport" content="width=device-width, initialscale=1">
This will help set better defaults for pages when viewed on a mobile device
3. Finally, the CSS, the JavaScript library, and jQuery itself need to be included
into the file.
Let's look at a modified version of our previous HTML file that adds all of these:
Listing 1-2: test2.html
<!DOCTYPE html>
<html>
<head>
<title>First Mobile Example</title>
<meta name="viewport" content="width=device-width, initialscale=1">
<link rel="stylesheet" href="jquery.mobile-1.4.5.min.css" />
<script type="text/javascript"
src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="jquery.mobile-1.4.5.min.js"></
script>
</head>
<body>
<h1>Welcome</h1>
<p>
Welcome to our first mobile web site.
It's going to be the best site you've ever seen.
Once we get some content. And a business plan.
But the hard part is done!
</p>
<p>
<i>Copyright Megacorp © 2015</i>
</p>
</body>
</html>
[6]
Chapter 1
For the most part, this version is the exact same as listing 1, except for the
addition of the DOCTYPE declaration, the CSS link, and our two JavaScript libraries.
Notice that we pointed to the hosted version of the jQuery library. It's perfectly fine
to mix local JavaScript files and remote ones. If you want to ensure that you can
work offline, you can simply download the jQuery library as well.
So, while nothing changed in the code between the body tags, there is going to be a
radically different view now in the browser. The following screenshot shows how
the iOS mobile browser renders the page now:
[7]
Right away, you see a couple of differences. The biggest difference is the relative
size of the text. Notice how much bigger and easier to read it is. As we said, the user
could have zoomed in on the previous version, but many mobile users aren't aware
of this technique. This page loads up immediately in a manner that is much more
usable on a mobile device.
In the previous HTML, the data-ray attribute is completely made up. However,
because our attribute begins with data-, it is also completely legal. So, what happens
when you view this in your browser? Nothing! The point of these data attributes is to
integrate with other code, like JavaScript that does whatever it wants with them. So,
for example, you could write JavaScript that finds every item in the DOM with the
data-ray attribute and change the background color to whatever was specified in
the value.
This is where jQuery Mobile comes in, making extensive use of data attributes both
for markup (to create widgets) and behavior (to control what happens when links
are clicked). Let's look at one of the main uses of data attributes within jQuery
Mobiledefining pages, headers, content, and footers:
Listing 1-3: test3.html
<!DOCTYPE html>
<html>
<head>
<title>First Mobile Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="jquery.mobile-1.4.5.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery2.1.3.min.js"></script>
[8]
Chapter 1
<script type="text/javascript" src="jquery.mobile-1.4.5.min.js"></
script>
</head>
<body>
<div data-role="page">
<div data-role="header"><h1>Welcome</h1></div>
<div role="main" class="ui-content">
<p>
Welcome to our first mobile web site.
It's going to be the best site you've ever seen.
Once we get some content. And a business plan.
But the hard part is done!
</p>
</div>
<div data-role="footer">
<h4>Copyright Megacorp © 2015</h4>
</div>
</div>
</body>
</html>
Compare the previous code snippet to listing 1-2 and you will see that the main
difference was the addition of the div blocks. One div block defines the page. Notice
that it wraps all of the content inside the body tags. Inside the body tag, there are
three separate div blocks. One has a role of header, another a role of content, and
the final one is marked as footer. The header and footer blocks use data-role,
which should give you a clue that we're defining a role for each of the blocks. The
center div block, the one for content, uses the role attribute instead of data-role
and adds a class. This is a special exception where jQuery Mobile (most recently) has
switched to using a class directly to help speed up the initial layout of the page. As
we stated earlier, these data attributes mean nothing to the browser itself, but jQuery
Mobile can recognize them and enhance them.
[9]
Notice right away that both the header and footer now have a gray background
applied to them. This makes them stick out even more from the rest of the content.
Speaking of content, the page text now has a bit of space between it and the sides.
The header and footer were enhanced automatically by the jQuery Mobile JavaScript
library, while the use of the ui-class style on the main content made use of the CSS
provided with the framework. This is a theme you will see repeated again and again
as we go through this book. A vast majority of the work you'll be doing will involve
the use of data attributes or a bit of CSS.
[ 10 ]
Chapter 1
Summary
In this chapter, we talked a bit about how web pages may not always render well in
a mobile browser. We talked about how the simple use of jQuery Mobile can go a
long way to improve the mobile experience of a website. Specifically, we discussed
how you can download jQuery Mobile and add it to an existing HTML page, what
data attributes mean in terms of HTML, and how jQuery Mobile makes use of data
attributes to enhance your pages.
In the next chapter, we will build upon this usage and start working with links and
multiple pages of content.
[ 11 ]
www.PacktPub.com
Stay Connected: