Hour 22. Implementing Mobile Web Pages


What You’ll Learn in This Hour:

Image Basic structure of mobile pages

Image How to link multiple mobile pages together

Image How to load additional mobile pages

Image Using navbars to navigate mobile sites

Image Implementing dialogs in mobile pages


A good mobile website will act more like a mobile application than a traditional website. To implement a website as a mobile app style, you need to think about things a bit differently. You don’t want a lot of controls, tables, or content all on the same page because mobile devices, even tablets, don’t have the screen area.

To build a good mobile website, you need to break up the content of the major pages into sections that compose “mobile” pages with mobile formatted content. Then you can apply simple transitions from one section to another using swipes, buttons, toolbars, and menus.

The following sections discuss creating web pages, linking them together, and then using jQuery navigational controls and events to transition between them.

Building Mobile Pages

Creating mobile pages is very simple. Pages consist of <div> elements that are enhanced in jQuery Mobile using the data tags discussed in the previous hour. This section focuses on using the data tags to define mobile web pages.

Understanding Page Anatomy

Mobile pages are composed of three main parts: the header, the footer, and content in between. All three are not necessarily required; however, it is a good idea to at least have a header with the content, especially when working with multiple pages.

All these elements are defined by adding data-role attributes to <div> elements. The content inside the <div> elements can be just about anything—text, images, forms, lists, and so on. The following shows the basic code for defining a mobile page with all three elements:

<div data-role="page">
  <div data-role="header"><h1>Header</h1></div>
  <div data-role="content" id="content">
    <p>Images</p>
    <img src="../images/flower.jpg" />
  </div>
  <div data-role="footer"><h4>Footer</h4></div>
</div>

Notice that each of the components has a distinct value for data-role. Figure 22.1 shows the resulting mobile web page.

Image

FIGURE 22.1 Basic mobile web page with a header, a footer, and a comment with text and images.

Everything else in mobile applications is based on these basic components. As you will see going forward, you can add buttons and toolbars to the header and footer and a wide array of elements to the content, but all mobile web pages start with these three components.

Creating Fixed Headers and Footers

Notice that in Figure 22.1, the footer is not at the bottom of the screen on the device. That is because it is flowing with the content. Both the header and the footer flow with the content, meaning if you scroll the content up or down, the header scrolls with the content, as shown in Figure 22.2.

Image

FIGURE 22.2 Setting the data-position="fixed" will make the header and footer stick to the device screen instead of scrolling with the content.

Flowing headers and footers are often a desired result; however, there are also many times that you want one or both to stay in a fixed position on the device screen so that it is always displayed. jQuery Mobile makes that adjustment very simple to make.

To make the header or footer—or a toolbar, for that matter—fixed, you add the data-position="fixed" attribute to the <div>. For example:

  <div data-role="header" data-position="fixed"><h1>Header</h1></div>
...
  <div data-role="footer" data-position="fixed"><h4>Footer</h4></div>

This will make them stay in place: the header at the top of the page and the footer at the bottom, regardless of how the content scrolls. This is illustrated in Figure 22.2.

Implementing Mobile Sites with Multiple Pages

Mobile sites are composed of either a single HTML document with multiple <div data-role="page"> elements or multiple HTML documents with those elements. Each <div> element represents a single mobile page.

When using multiple pages in your mobile website, you need to implement code and UI controls to provide ways for the user to transition from one mobile page to another. The transitions should be smooth and intuitive based on the controls and content interaction.

Changing mobile pages is accomplished by linking to the second page from the first. This can be done using one of two methods: adding navigation buttons or programmatically changing the page in your jQuery code.

The pages can come from <div> elements in the same web page or external URLs downloaded to the device. The following sections describe the methods of implementing multiple page mobile sites.

Adding Navigation Buttons

Navigation buttons are links to other mobile pages. Typically, this is done in the header or footer element for easy visibility. However, you can also place them inside the mobile content.

The sole purpose of the navigation buttons is to allow the user to move from one mobile page to the next, so it is important to implement them with that in mind.

Creating Navigation Buttons

Navigation buttons are created by adding the data-role="button" attribute to an <a> link. The href attribute should point to the hash tag or URL of the mobile page you want to switch to. An example of the navigation buttons is shown in Figures 22.3 and 22.4. The following code shows the syntax for defining an <a> tag as a navigation link:

<a href="#page2" data-role="button">Next</a>
<a href="hour2201-page3.html" data-role="button">Next</a>

Image

FIGURE 22.3 Linking to another page already loaded in the DOM.

Image

FIGURE 22.4 Linking to another mobile page on the web server.

Positioning Navigation Buttons

You can position the button on the left or right side of the header or footer, adding the .ui-btn-right or .ui-btn-left class to the <a>.

The following code shows some examples of the navigation button links:

<a href="#page2" data-role="button" class="ui-btn-right">Next</a>
<a href="hour2201-page3.html" data-role="button" class="ui-btn-left">Next</a>

Creating a Back Button

Another useful feature included in jQuery Mobile is the capability to define a navigation button as a back button. A back button uses the browser history to navigate to the previous button mobile page. To define a link as a back button, you need to add the data-rel="back" attribute. For example:

<a data-rel="back data-role="button" class="ui-btn-right">Back</a></div>

Notice that there is not an href attribute. That is because the href attribute will be ignored; instead, the most recent URL will be popped off the browser’s navigation history list.

Changing Pages with jQuery Code

The second method is to use the $.mobile.changePage(URL, options) function call where the URL is the link location. Table 22.1 shows the available options for the .changePage() call. The following is an example of adding a swipeleft event handler to load a remote web page when the user left-swipes the page on the device. Notice that a transition of “slide” is used, and reverse option is set to true:

$("#pageTwo").on("swipeleft", function(){
  $.mobile.changePage("newPage.html", {transition:"slide", reverse:true}); });

Image

TABLE 22.1 Options for the .changePage() and .loadPage() Calls

Loading Mobile Pages Without Displaying Them

Another helpful function is the .loadPage(URL, options) function. Load page downloads the mobile page from the web server using an AJAX call, but does not change the mobile page to the downloaded one. Actually, .changePage() calls .loadPage() underneath to retrieve the page. Most of the options listed in Table 22.1 are also available via .loadPage(), except changeHash, dataUrl, reverse, and transition.

The .loadPage() function is useful to preload pages in the initialization functions that you want available later but do not want to display yet. The following code shows an example of loading a page using POST data from a form:

$.mobile.loadPage("newPage.php", {data=$("form").serialize(), type="post"});

Events Triggered by Changing Pages

An important feature included with jQuery Mobile is the capability to trigger and handle events linked to changing and loading pages.

When you change pages, the following events are triggered:

Image pagebeforechange, pagechange, pagebeforeload, pageload, pageshow, pagehide

When you load pages, the following events are triggered:

Image pagebeforeload, pageload

These events allow you to implement code to handle new pages being transitioned and prevent pages from being downloaded from the server. The events are implemented as standard jQuery events, and the object passed back to the handler includes things like url, absUrl, dataUrl, and xhr objects, as well as the options used for changing pages.

The following shows an example of adding a pageload event handler:

$(document).on("pageload", function(e, obj){
  if($("#pageThree .ui-content").length) {
    $("#pageThree .ui-content").append("Page loaded from ."+ obj.url); }
});

Linking to Other Mobile Pages

Linked mobile pages displayed on the device can be prepared and displayed in one of three ways, which are listed next and are described in more detail in the sections that follow:

Image Included in the original HTML document and linked by a local hash.

Image Downloaded via AJAX request and inserted into the original DOM.

Image Downloaded via non-AJAX request (traditional) and displayed as a new page.

The first two options provide the advantage that you are working with a single DOM. The benefits are that the libraries and CSS code are loaded only once, and it is easier to share data between pages because everything stays in memory. The downside is that for complex pages, the single DOM method may take a lot of browser memory because all pages remain in memory.

Local Hash

The simplest method in linking to another page is using a local hash. To do this, the page must already be loaded in the DOM. You can then link to the page by adding an <a> tag with the data-role="button" attribute set and an href value pointing to the id of the desired page.

For example, the following code adds a link button to the header of #pageOne that links to a second mobile page #pageTwo as shown in Figure 22.3:

<div data-role="page" id="pageOne">
  <div data-role="header"><h1>Page 1</h1>
    <a href="#pageTwo" data-role="button" class="ui-btn-right">Next</a></div>
...
</div>
<div data-role="page"  id="pageTwo">
...

AJAX DOM Insertion

This is the default functionality for <a> tags and .changePage() handling external URLs. The AJAX insertion occurs when you pass an external URL in the <a> tag or the .changePage() call. jQuery Mobile will first load the document from the web server, enhance it for jQuery Mobile, and then insert it into the DOM. Then the current page will be changed over to the new page.


Caution

In jQuery Mobile, AJAX is used to load the contents of external pages into the DOM as you navigate, but the DOM ready handler executes for only the first page. Therefore, to execute code whenever a new page is loaded and created, you need to put the initialization code in $(document).on('pageinit') instead of $(document).ready().


For example, the following code adds a link button to the header of #pageOne that links to a second mobile page #pageThree that is in a separate HTML document, as illustrated by Figure 22.4:

<div data-role="page" id="pageOne">
  <div data-role="header"><h1>Page 1</h1>
    <a href="hour2201-page3.html" data-role="button"
       class="ui-btn-right">Next</a></div>
...

HTML snippet file on web server:

<div data-role="page" id="pageThree">
  <div data-role="header"><h1>Page 3</h1></div>
  <div data-role="content">
    <p>New Day</p><img src="../images/sunstar.jpg" /></div>
  <div data-role="footer"><h4>Footer</h4></div>
</div>


Note

In reality, the best solution is to mix these two methods. Use a single DOM as much as possible and then load a fresh DOM when working with the larger amount of content.


Bypassing AJAX

At times, you do not want to have the mobile page inserted into the DOM. For example, when you have a large amount of data being transferred with pages, you might not want to have all of them loaded at once.

To prevent the AJAX DOM insertion when linking to a web page, you can use a target attribute in the <a> tag or specify a rel="external" or data-ajax="false". For example:

<a href="hour2201-page3.html" data-role="button" target="_blank">New</a>

or

<a href="hour2201-page3.html" data-role="button" data-ajax="false">New</a>

or

<a href="hour2201-page3.html" data-role="button" rel="external">New</a>

In these instances, a normal HTTP request will be made to load the HTML link as a normal web page.

Adding Page Transitions

A great feature of linking pages is the animated transitions between them. This can be extremely useful in giving users a better experience. A good example of this is when you swipe to change pages and can swipe either left or right. It is nice to have the new page slide in the direction of the swipe, as if the user actually pulled the page in from off the device screen.

Transitions are added to the page links either by adding the data-transition attribute to the <a> tag or setting the transition option in the .changePage() call. You can also use the data-direction="reverse" attribute or reverse:true option to specify that a transition should happen in reverse, such as a slide backward. An example of using setting the page transition is shown next:

$.mobile.changePage("#pageTwo", {transition:"slide" });
<a href="#pageTwo" data-role="button" data-transition="slide">Next</a>
$.mobile.changePage("#pageOne", { reverse:true});
<a href="#pageOne" data-role="button" data-direction="reverse">Back</a>

Creating a Navbar

Another method of navigating pages is using a navbar. A navbar is a set of buttons grouped together in a single bar element. Each button links to a different mobile page.

Navbars are defined by adding the role="navbar" to a div and then adding a list of pages to link to using <ul>, <li>, and <a> elements. For example, the following code will render a navbar similar to the one in Figure 22.6:

<div data-role="navbar" >
  <ul>
    <li><a href="#pageTwo">page 2</a></li>
    <li><a href="#pageThree">page 3</a></li>
    <li><a href="#pageFour">page 4</a></li>
  </ul>
</div>

Image

FIGURE 22.6 Mobile web page with navbar below the header and as a fixed footer.

The navbar <div> can be placed anywhere. You can put it in the header, footer, content, or it can stand alone between the other sections of the mobile page. A common place to put the navbar is in a fixed footer. This allows the navbar to remain present even as the content scrolls.

Implementing Dialogs

Another type of page in jQuery Mobile is the dialog. A dialog is just a mobile page that is presented as a dialog using the data-rel="dialog" attribute in the <a> link. Instead of displaying the page as a normal full page, it will be displayed as a dialog with a close button in the header.

The following code shows an example of a dialog link in the header of a mobile page with the data-rel="dialog":

<a href="#imageDialog" data-role="button" class="ui-btn-right"
   data-rel="dialog" data-icon="gear">Options</a>

When the link above calls is clicked, the following mobile page is loaded as a dialog, as shown in Figure 22.8:

div data-role="page" id="imageDialog">
  <div data-role="header"><h1>Select Image</h1></div>
  <div data-role="content">
    <a href="#pageOne" data-role="button"><img src="../images/lake.jpg" />Lake</a>
    <a href="#pageTwo" data-role="button"><img src="../images/peak.jpg" />Peak</a>
    <a href="#pageThree" data-role="button"><img src="../images/river.jpg"
/>River</a>
  </div>
</div>

Image

FIGURE 22.8 Page loaded as a dialog box.

A dialog is not treated as a page link as far as the browser history is concerned; it is just an overlay on top of the page with the <a> link that launched it, so pressing the back button will navigate to the previous page, not back to the calling page.

You can chain multiple dialog pages together, one linking to the other. Closing subsequent dialogs will automatically link back to the previous dialog.


Note

The dialog UI container is styled by the .ui-dialog-contain class. It has a width of 92.5 to render the dialog smaller than the original page.


Summary

In this hour, you learned about mobile web pages. You learned how the parts of the mobile page are header, footer, content, and occasionally a navbar. You learned how to link multiple mobile pages together by adding navigation links to the headers and footers, or by using a navbar with a list of pages to link to. You also implemented dialog boxes.

Q&A

Q. Is there a way to cache mobile pages in the DOM so I don’t have to keep reloading them?

A. Yes, you can use the following setting to enable the DOM cache. This will enable caching for all pages.

$.mobile.page.prototype.options.domCache = true;

You can also add the data-dom-cache="true" attribute to the page if you want to cache only specific pages. For example:

<div data-role="page" id="cacheMe" data-dom-cache="true">

Additionally you can do it programmatically using the following:

pageContainerElement.page({ domCache: true });

Workshop

The workshop consists of a set of questions and answers designed to solidify your understanding of the material covered in this hour. Try to answer the questions before looking at the answers.

Quiz

1. How do you define a <div> as a page header?

2. How do you add transitions to page changes?

3. True or False: A jQuery Mobile link to an external HTML document will not load the scripts in that file.

4. How do you define a page as a dialog box?

5. Name three events that are triggered by a page change.

Quiz Answers

1. Add the data-role="header" attribute.

2. Add the data-transition attribute in the <a> link that points to the page.

3. True. The jQuery Mobile page load defaults as an AJAX request.

4. Use the data-rel="dialog" attribute in the <a> link that points to the page.

5. beforepageload, pageload, beforepagechange, pagechange, pagehide, and pageshow.

Exercises

1. Open the code in Listing 22.1. Add an id attribute to the <img> tag. Then in the jQuery .ready() function, add a click event that resizes the image. For example:

$("#img1").on("click", function(){
  $(this).css({width:400})
});

2. Open the code in Listing 22.3 and add an additional mobile page to the HTML. Then add a link from the #options dialog to that page. The link should include the data-rel="dialog" so that your page opens as a dialog.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset