Return to Snippet

Revision: 51121
at September 15, 2011 01:38 by rydaly


Initial Code
// create an object named "app" which we can define methods on
var app = {
    // returns an array of each url to prefetch
    prefetchLinks: function(){
        // returns an array of each a.prefetch link's href
        var hrefs = $("a.prefetch").map(function(index, domElement){
            return $(this).attr("href");
        });
        // returns the array of hrefs without duplicates
        return $.unique(hrefs);
    },

    // adds a link tag to the document head for each of prefetchLinks()
    addPrefetchTags: function(){
        // for each prefetchLinks() ...
        this.prefetchLinks().each(function(index,Element){
            // create a link element...
            $("<link />", {
                // with rel=prefetch and href=Element...
                rel: "prefetch", href: Element
                // and append it to the end of the document head
            }).appendTo("head");
        });
    },
}
// when the document is ready...
jQuery(function(){
  // call the method we defined above.
    app.addPrefetchTags();
}

Initial URL
http://www.catswhocode.com/blog/mastering-html5-prefetching

Initial Description
//#########################################################################
//
// *taken from http://www.catswhocode.com/blog/mastering-html5-prefetching
//
//#########################################################################

Initial Title
HTML5 prefetch / prerender with jQuery

Initial Tags
jquery, html5

Initial Language
jQuery