Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
jQuery Mobile Cookbook
jQuery Mobile Cookbook

jQuery Mobile Cookbook: Over 80 recipes with examples and practical tips to help you quickly learn and develop cross-platform applications with jQuery Mobile book and ebook.

eBook
$25.99 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

jQuery Mobile Cookbook

Chapter 2. Pages and Dialogs

In this chapter, we will cover:

  • Writing a single-page template application

  • Writing a multi-page template application

  • Prefetching pages for faster navigation

  • Using the DOM cache to improve performance

  • Custom styling a dialog

  • Using CSS to create a bouncing page transition

  • Using JS to create a slide and fade page transition

  • Using data-url to handle a login page navigation

  • Using the History API to create a custom error pop up

Introduction


A Page is the basic jQuery Mobile object written within a <div data-role="page"> container that gets displayed on the screen. It can contain the header, the page content, and the footer. You can embed various HTML5 controls and widgets within a page. The jQuery Mobile framework automatically enhances and displays all these controls, making them tap-friendly (finger-friendly). Your application can have a series of individual HTML files each representing a single page, or it can have one single HTML file containing multiple page div containers within it. You can provide links to open other pages within a page, and when the user clicks on a link, the new page opens using Ajax with CSS3 animation. The current page is then hidden from view.

A Dialog is a page having the data-role="dialog" attribute. You can also load a page as a dialog by adding the data-rel="dialog" attribute to the page link. The dialog is styled differently from a page, and it appears in the middle of the...

Writing a single-page template application


In a single-page template application, each page of the application will have its own HTML file. A page is wrapped within a page container as <div data-role="page">. When you launch the app, the jQuery Mobile framework will load the first page of the app (or the main page) into the DOM, whose reference is held all through the app cycle. The main page just gets hidden when the user navigates to another page, which now is marked as an active page. Except for the main page, all other pages get removed from the DOM when the user navigates away from them. Navigation between the pages is specified using anchor links. The anchor links are decorated as buttons using the data-role="button" attribute. On clicking any link, navigation occurs with some cool CSS3 transitions, and the new page is pulled in via Ajax.

This recipe shows you how to create a single-page template application and navigate between the pages of the app.

Getting ready

Copy the full...

Writing a multi-page template application


In a multi-page template application, the HTML file will have multiple pages in it. Each page is wrapped within a page container as <div data-role="page">. The page ID is used to identify the pages for linking or invoking any actions on them. The page ID must be unique within your app. When you launch the app, the jQuery Mobile framework loads all the available pages into the DOM and displays the first page it finds in the HTML. Navigation between the pages is specified by using anchor links, and you can decorate these links as buttons by using the data-role="button" attribute. On clicking any link, navigation occurs with some cool CSS3 transitions, and the new page is pulled in via Ajax. This recipe shows you how to create a multi-page template application and navigate between the multiple pages it contains.

Getting ready

Copy the full code of this recipe from the code/02/multi-page sources folder. You can launch this code using the URL http...

Prefetching pages for faster navigation


Using a single-page template for your mobile app makes your mobile app faster and lighter. But you have to fetch each page during navigation. You can see the ui-loader spinning icon every time a page loads. This problem does not happen with a multi-page template application, as all the pages are already preloaded into the DOM. By using the prefetch feature, a single-page template application can be made to mimic the multi-page template application.

A page marked for prefetch is loaded in the background and is immediately available when the user tries to open it. You can prefetch pages in two ways. The first is by just adding the data-prefetch attribute to the anchor link. The second way is by using JavaScript to call the loadPage() method. This recipe shows you how to improve page loading speed by prefetching pages in your jQuery Mobile app.

Getting ready

Copy the full code of this recipe from the code/02/prefetch sources folder. You can launch this...

Using the DOM cache to improve performance


During page navigation in a single-page template application, each new page is fetched and stored in the DOM. The page remains in the DOM and is removed once you navigate away from the page. Only the main or the first page of the app always remains in the DOM. As seen in the previous recipe, prefetching commonly-used pages could help in improving performance to some extent. But when you visit a prefetched page and navigate away from it, the page gets removed from the cache. So the problem of multiple fetching of frequently visited pages is not fully solved.

With DOM caching, specific pages are marked to be cached in the DOM. These pages, once loaded, remain in the DOM all through the life cycle of the app. You can use the DOM cache in two ways. The first is by adding the data-dom-cache attribute to the page container of the page that is to be cached. The second way is by using JavaScript. This recipe shows you how to improve the performance of your...

Custom styling a dialog


You can style a page as a dialog by using the data-role="dialog" attribute on the page container. You can also specify the data-rel="dialog" attribute in the anchor link used to open the page. The page now gets styled as a dialog, and opens with a pop transition. When you add a header to the dialog, a close icon is created on the header, by default, in the left side of the header. In some applications/platforms, you might want to position this close button on the right side of the header. There is no ready option available to change this icon's position. This recipe shows you how to build a dialog with a custom styled header to position the close button at the right side of the header.

Getting ready

Copy the full code of this recipe from the code/02/custom-dialog sources folder. You can launch this code using the URL http://localhost:8080/02/custom-dialog/main.html.

How to do it...

The steps to be followed are:

  1. Create main.html with the #main page. Add a link here to...

Using CSS to create a bouncing page transition


As you navigate between the pages of your app, the jQuery Mobile framework uses CSS3 animations to show some cool transition effects. The fade transition is used by default for pages, and the pop transition is used for dialogs. You can navigate into a page with a particular transition, and when you navigate out of the page, you can reverse the direction of the transition. jQuery Mobile comes with a default set of 10 transitions as of v1.1.1. The jQuery Mobile online docs has a nice online demo showing all the available transitions. But that's not all; you can use CSS to create your own custom transitions and use them in your app. This recipe shows you how to use CSS and create a bouncing page effect during page transitions.

Getting ready

Copy the full code of this recipe from the code/02/custom-css-transition sources folder. You can launch this code using the URL http://localhost:8080/02/custom-css-transition/main.html.

How to do it...

The steps...

Using JS to create a slide and fade page transition


In the previous recipe, you learned to add a custom transition to your jQuery Mobile app using CSS. You can also create custom transitions using JavaScript. This recipe shows you how to create a "slidefade" (slide and fade) effect during page transition in your app by using JavaScript.

Getting ready

Copy the full code of this recipe from the code/02/custom-js-transition sources folder. You can launch this code using the URL http://localhost:8080/02/custom-js-transition/main.html.

How to do it...

Carry out the following steps:

  1. Create the customtransition.js JavaScript file and define your custom transition by adding a mycustomTransition() method, as shown in the following code snippet. Here, define how the from and to pages should animate during the transition:

    function mycustomTransition( name, reverse, $to, $from ) {
        var deferred = new $.Deferred();
        // Define your custom animation here
        $to.width("0");
        $to.height("0");
        $to...

Using data-url to handle the login page navigation


When you write a login page in your app, once the user enters valid credentials, you will want to redirect the user to a different page or to a different folder on success. This recipe shows you how to redirect the user to a different page during a login page navigation scenario, by using the data-url attribute.

Getting ready

Copy the full code of this recipe from the code/02/data-url sources folder. You can launch this code using the URL http://localhost:8080/02/data-url/login/main.html.

How to do it...

The steps to be followed are:

  1. Create two folders called login and records. The login folder will contain main.html, and the records folder will contain the index.html and data.html files.

  2. In the login folder, create main.html as a multi-page document. Here, first add the #main page as shown in the following code snippet. Also add a link to open the #login page.

    <div data-role="page" id="main">
      <div data-role="header">
        <h1...

Using History API to create a custom error pop up


The jQuery Mobile framework does not track dialogs in history. A dialog will thus not reappear when you click on the back button of your browser. Using a dialog for some features, for example to show an error pop up or an alert, has a minor issue that is very visible. When the dialog is opened from a page, the address bar will show the page URL suffixed with the #&ui-state=dialog text. This might not be desirable to all. This recipe shows you how to use the History API and customize a regular dialog to appear, such as a pop up without any changes to the URL, making use of the History API.

Getting ready

Copy the full code of this recipe from the code/02/history sources folder. You can launch this code using the URL http://localhost:8080/02/history/main.html.

How to do it...

The steps to be followed are:

  1. Create main.html, and add a link to open the errordialog.html file as a dialog. Also add an input button, as shown in the following code snippet...

Left arrow icon Right arrow icon

Key benefits

  • Create applications that use custom animations and use various techniques to improve application performance
  • Use and customize the various controls such as toolbars, buttons, and lists with custom icons, icon sprites, styles, and themes
  • Write simple but powerful scripts to manipulate the various configurations and work with the events, methods, and utilities which are provided by the framework

Description

jQuery Mobile is an award winning, HTML5/CSS3 based open source cross-platform UI framework. It offers a very cool and highly customizable UX. It is built on the popular jQuery library and uses declarative coding making it easy to use and learn. It is the market leader today considering the numerous browsers and platforms that it supports."jQuery Mobile Cookbook" presents over a hundred recipes written in a simple and easy manner. You can quickly learn and start writing code immediately. Advanced topics such as using scripts to manipulate, customize, and extend the framework are also covered. These tips address your common everyday problems. The book is very handy for both beginner and experienced jQuery Mobile developers.You start by developing simple apps using various controls and learn to customize them. Later you explore using advanced aspects like configurations, events, and methods.Develop single and multi-page applications. Use caching to boost performance. Use custom transitions, icon sprites, styles, and themes. Learn advanced features like configurations, events, and methods. Explore future trends by using HTML5 new features and semantics with jQuery Mobile."jQuery Mobile Cookbook" is an easy read and is packed with practical tips and screenshots.

Who is this book for?

If you are a beginner with jQuery/JavaScript skills, this book offers you numerous examples to get you started.If you are a seasoned developer, this book lets you explore jQuery Mobile in greater depth.

What you will learn

  • Create single-page and multi-page applications that use custom CSS and JavaScript transitions; improve performance using Prefetch, DOM-Cache, and Application Cache
  • Use fixed and full screen toolbars, navbars, and buttons; customize them with your own icons, icon sprites, and styles
  • Use XML and JSON data in your application; format page content using layout grids, collapsibles, and nested accordions
  • Build accessible forms; use form controls like flip switches, sliders, and select menus; validate and submit forms using Ajax
  • Use various types of lists such as Inset, Numbered, Nested, Read-only, and Split Button lists; manipulate lists using JavaScript
  • Use JavaScript to dynamically create and initialize controls, load and change pages, handle events; tweak and customize the framework configurations
  • Explore HTML5 semantics and features such as Local Storage, Session Storage, History, 2D Canvas, 3D, Geolocation, Web Workers, Audio, and Video
  • Use custom fonts and backgrounds, upgrade themes, override existing themes; generate and share new themes using the Theme Roller tool
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 14, 2012
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517225
Category :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Nov 14, 2012
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781849517225
Category :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 92.98
jQuery Mobile Cookbook
$48.99
Responsive Web Design with HTML5 and CSS3
$43.99
Total $ 92.98 Stars icon

Table of Contents

11 Chapters
Get Rolling Chevron down icon Chevron up icon
Pages and Dialogs Chevron down icon Chevron up icon
Toolbars Chevron down icon Chevron up icon
Buttons and Content Formatting Chevron down icon Chevron up icon
Forms Chevron down icon Chevron up icon
List Views Chevron down icon Chevron up icon
Configurations Chevron down icon Chevron up icon
Events Chevron down icon Chevron up icon
Methods and Utilities Chevron down icon Chevron up icon
The Theme Framework Chevron down icon Chevron up icon
HTML5 and jQuery Mobile Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(1 Ratings)
5 star 0%
4 star 0%
3 star 100%
2 star 0%
1 star 0%
kljourney Jan 27, 2013
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
I wa a bit disappointed - not enough clear examples and recipes for real world development. I was hoping to see more but it's like a lot if the other books on the subject.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela