The jQuery Mobile "page" structure is optimized to support either single pages, or local internal linked "pages" within a page.
The goal of this model is to allow developers to create websites using best practices — where ordinary links will "just work" without any special configuration — while creating a rich, native-like experience that can't be achieved with standard HTTP requests.
A jQuery Mobile site must start with an HTML5 'doctype' to take full advantage of all of the framework's features. (Older devices with browsers that don't understand HTML5 will safely ignore the 'doctype' and various custom attributes.) In the 'head', references to jQuery, jQuery Mobile and the mobile theme CSS are all required to start things off:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="ui-theme.css" />
<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript" src="jquery-mobile-min.js"></script>
</head>
<body>
...
</body>
</html>
Inside the <body> tag, each view or "page" on the mobile device is identified with an element (usually a div) with the data-role="page" attribute:
<div data-role="page">
...
</div>
Within the "page" container, any valid HTML markup can be used, but for typical pages in jQuery Mobile, the immediate children of a "page" are divs with data-roles of "header", "content", and "footer".
<div data-role="page">
<div data-role="header">...</div>
<div data-role="content">...</div>
<div data-role="footer">...</div>
</div>
Putting it all together, this is the standard boilerplate page template you should start with:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="ui-theme.css" />
<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript" src="jquery-mobile-min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">...</div>
<div data-role="content">...</div>
<div data-role="footer">...</div>
</div>
</body>
</html>
jQuery Mobile automates the process of building Ajax powered site and applications.
By default, when you click on link that points to an external page (ex. products.html), the framework will parse the link's href to formulate an Ajax request (Hijax) and displays the loading spinner.
If the Ajax request is successful, the new page content is added to the DOM, the mobilize() function runs, then the new page is animated into view with a page transition.
If the Ajax request fails, the framework will display a small error message overlay (styled in the "e" swatch) that disappears after a brief time so this doesn't break the navigation flow. View an example of the error message.
A single HTML document can contain either a single 'page' or multiple 'pages' can be assembled and loaded together by stacking multiple divs with a data-role of "page". This allows you to build a small site or application within a single HTML document; jQuery Mobile will simply display the first 'page' it finds in the source order when the page loads.
If a link points to an anchor (#foo), the framework will looks for a page with that ID. If it finds a page in the HTML document, it will transition the new page into view.
Here is an example of a 2 "page" site built with two jQuery Mobile divs navigated by linking to an ID placed on each page wrapper. Note that the IDs on the page wrappers are only needed to support the internal page linking, and are optional if each page is a separate HTML document.
<div data-role="page" id="foo"> <div data-role="content"> I'm the "foo" page. Since I'm the first page in the source order, I will be displayed onLoad. <a href="#bar">Visit the bar "page"</a> </div><!-- /content --> </div><!-- /foo page --> <div data-role="page" id="bar"> <div data-role="content"> I'm the "bar" page. I will be shown only if the anchor link on the <a href="#foo">foo</a> page is clicked. </div><!-- /content --> </div><!-- /bar page -->
You can seamlessly navigate between local, internal "pages" and external pages in jQuery Mobile. Both will look the same to the end user except that external pages will display the Ajax spinner while loading. In either situation, jQuery Mobile updates the page's URL hash to enable Back button support, deep-linking and bookmarking.
PLEASE NOTE: Since we are using the hash to track navigation history for all the Ajax 'pages', it's not currently possible to deep link to an anchor (index.html#foo) on a page in jQuery Mobile, because the framework will look for a 'page' with and ID of #foo instead of the native behavior of scrolling to the content with that ID.
Learn more about the technical details of the navigation model and Ajax, hashes and history in jQuery mobile.