In jQuery Mobile there are two ways to structure webpages for jQuery Mobile: incorporate all the pages in the same file, or create separate files for each page like a typical website. Understanding the different page template types is the foundation for customizing a number of different page-related functionalities. In addition to learning about the page template types and creating multipage websites in jQuery Mobile, you’ll also see how to preload and cache pages, work with different page transitions, and customize loading messages. You’ll learn to create custom functionality to take your pages to the next level.
Internal linking occurs automatically when you have multiple jQuery Mobile pages in the same HTML file. As you’ve learned, jQuery Mobile pages are defined by adding a data-role with a value of page to an HTML element and anything within that page becomes relative to that page. In jQuery Mobile, typical separate webpages are considered single-pagetemplates, while webpages that contain multiple pages are considered multipagetemplates. Let’s refer back to our multipage template example from the previous chapter (with a few small additions):
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Multipage template - jQuery Mobile: Design and Develop</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<div data-role="page" id="page-one" data-title="Page 1">
<p>Body copy for page 1</p>
<p><a href="#page-two">Link to page 2</a></p>
<div data-role="page" id="page-two" data-title="Page 2">
<p>Body copy for page 2</p>
<a href="#page-one">Link to page 1</a>
Within this multipage template are two pages defined by div elements with custom ids. The jQuery Mobile framework shows only one of these pages at a time, and it uses thedata-title attribute to change the page title dynamically. The page that is shown by default is determined by the order of the source code. In this example, the first page has an id of page-one, but if the pages in this file were switched, so that the element with anid of page-two came first, then that would be the default page to load. In other words, the value of the id attribute doesn’t determine what page is shown by default; the default page is determined only by the source code order. However, the id is used for other important purposes, such as linking pages to one another.
- Create a hyperlink.
- Type the pound sign (#).
- Specify the id value of the page you want to link to.
The end result looks like this:
<a href="#page-two">Link to page 2</a>
It’s similar to creating a page anchor; the difference is that you’re referencing the id value of another page. Remember that each page needs a unique id value. In this case, I’ve usedpage-one and page-two, but you can use something more descriptive and relative to the content of your jQuery Mobile page. The cool thing about the jQuery Mobile framework is that it transitions dynamically from one page to another without requiring you to write an ounce of code. Figure 4.1shows an example of this code in both page views and the transition from one to another.
Figure 4.1. A multipage template with two pages and the transition in between.