Page structure

Following HTML markup conventions is essential to this approach because markup is the primary mechanism used to configure the mobile experience in jQuery Mobile. Validate your code and always avoid the use of inline styles or inline JavaScript event handlers to ensure the best results.

Mobile page structure

A jQuery Mobile site starts with an HTML5 'doctype'. 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="..." /> 
		<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> 

External page linking

Most sites or applications will consist of multiple HTML pages that are linked. As long as each page in the standard jQuery Mobile structure, to link between them, simply data-role of "page", multiple 'pages' can be assembled and loaded together. This allows you to build a small site or application within a single document and jQuery Mobile will simply display the first 'page' it finds in the source order when the page loads.

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.

Local, internal linked "pages"

A single HTML document can contain either a single 'page' or, by stacking multiple divs with a data-role of "page", multiple 'pages' can be assembled and loaded together. This allows you to build a small site or application within a single document and 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 UI. 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.

Page transitions

The jQuery Mobile framework includes a set of CSS-based transitions that can be applied to any object or page change event. By default, when navigating to a new page, the framework applies the right to left slide transition effect. When via the Back button, the slide transition direction is reversed.

Learn more about the technical details of the Page-Navigation-Model.

To set a custom transition effect, add the data-transition attribute to the link. Possible values include:


<a href="index.html" data-transition="pop">I'll pop</a>
slide slideup slidedown pop flip fade

Dialogs

Any page can be presented as a dialog by adding the data-rel="dialog" attribute to the page you're linking to. The framework will add styles to make the page look like a modal dialog by adding rounded corners and margins around the page along with a dark background behind the "dialog".

The dialog will still open with the standard slide transition so to make it feel more dialog-like, we recommend adding a transition of pop, slideup or flip on the link too.


<a href="../_dialog.html" data-role="button" data-inline="true" data-rel="dialog" data-transition="pop">Open dialog: pop</a>
Open dialog: pop Open dialog: slideup Open dialog: flip