jQuery Mobile : Page pre-fetch

 The latest release of jQuery Mobile beta 2 is characterized by a new added feature. This feature is called Page pre-fetch, which finds the links of the active pages and makes a lazy load for these pages into the DOM beyond the scene before the user clicks on it.

With this feature we don't need to add all pages on the same HTML documents. In the previous releases, this feature works automatically when the user adds the pages with the same file.

Let's start with a simple example to illustrate how it works.

The first page is:

Page1.html

<!DOCTYPE html >

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title></title>

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />

<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>

<script src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>

</head>

<body>

    <div id="page1" data-role="page">

        <div data-role="content">

            <a href="Page2.html" data-prefetch>Link</a>

        </div>

    </div>

</body>

</html>

 

And the second page is:

Page2.html

 <!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div id="page2" data-role="page">
        <div data-role=" content">
            This is page 2 loaded in the DOM by Page pre-fetch option
        </div>
    </div>
</body>
</html>

 

Notice that the anchor of the first page has a data-prefetch option. Try to open "page1"; the DOM would be like this:

 


Here you can find that "page1" is active and the html of "page2" is added to the DOM, even though it is in separate HTML documents and it is not called before.

According to this feature, after clicking on the link "Page2" it will be displayed immediately without the Ajax loader.

Note: But it's so important to use this option only with highly likely that the link will be visited by users due to Pre-fetching increased bandwidth by using additional HTTP requests.

 

 

Add pre-fetch option programmatically:

 

In this case we can use $.mobile.loadPage method. This method loads the external page into DOM without any visual effect on the current active page.

Example:

<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>
<script type="text/javascript">   
        $(window).load(function () {
           $.mobile.loadPage("Page2.html");      
    });
</script>
</head>
<body>
    <div id="page1" data-role="page">  
        <div data-role="content">
            <a href="Page2.html" >Link</a>
        </div>
      
    </div>
</body>
</html>
 
 
 

$.mobile.loadPage method is the core of this feature which used by jQuery framework, here you can find how it works and how it finds the links in the active page:

//prefetch pages when anchors with data-prefetch are encountered
          $( ".ui-page" ).live( "pageshow.prefetch"function(){
                    var urls = [];
                    $( this ).find( "a:jqmData(prefetch)" ).each(function(){
                               var url = $( this ).attr( "href" );
                               if ( url && $.inArray( url, urls ) === -1 ) {
                                         urls.push( url );
                                         $.mobile.loadPage( url );
                               }
                    });
          } );
 
 
 
Hope that help.
Published Sunday, August 14, 2011 8:43 AM by Yousef_Jadallah
Filed under: , ,

Comments

# jQuery Mobile : Page pre-fetch

Sunday, August 14, 2011 9:20 AM by jQuery Mobile : Page pre-fetch

Pingback from  jQuery Mobile : Page pre-fetch

# re: jQuery Mobile : Page pre-fetch

Tuesday, August 23, 2011 4:28 AM by Testbaudson

Thanks for the summary, has been helpful, especially the reference to loadPage

# re: jQuery Mobile : Page pre-fetch

Sunday, August 28, 2011 9:48 PM by Rick

Any idea what happens if a page has multiple links to the "next" page I want to pre-fetch? For example, I have a link to the next page at the top and bottom of the current page... Do I need to put the prefetch attribute on both links? If so Does jQuery recognize that it is the same URL and only cache it once?

# re: jQuery Mobile : Page pre-fetch

Monday, August 29, 2011 6:39 AM by Yousef_Jadallah

Even though the page has more than one pre-fetch link target to the same page, it will be loaded once on the DOM.

# re: jQuery Mobile : Page pre-fetch

Monday, August 29, 2011 3:23 PM by Rick

Awesome! Thanks Yousef.

# re: jQuery Mobile : Page pre-fetch

Monday, August 29, 2011 9:47 PM by Rick

Awesome! Thanks, Yousef.

# re: jQuery Mobile : Page pre-fetch

Tuesday, December 13, 2011 7:28 AM by Dayo

Hi found this after several hours of hard Googling.

I posted a query on StackOverflow on this sort of issue that is getting no love: stackoverflow.com/.../jquery-selector-validity-scope

The main point is that I want to preload hrefs from links in the head section. Possible? If not, I could look into moving them into the main body.

Will really appreciate some tips and apologise for crashing this.

Thanks

# re: jQuery Mobile : Page pre-fetch

Monday, March 18, 2013 10:46 PM by Sheldon

you are actually a good webmaster. The website loading pace is incredible.

It kind of feels that you are doing any distinctive trick.

Furthermore, The contents are masterwork. you've performed a wonderful process on this subject!

# re: jQuery Mobile : Page pre-fetch

Monday, March 25, 2013 10:54 AM by Venegas

I always spent my half an hour to read this weblog's posts every day along with a cup of coffee.replica louis vuitton bags

# re: jQuery Mobile : Page pre-fetch

Tuesday, March 26, 2013 11:50 PM by Shelby

Usually I do not read article on blogs, however I wish to say that this write-up very forced me to

check out and do so! Your writing taste has been surprised me.

Thank you, very great post.

Leave a Comment

(required) 
(required) 
(optional)
(required)