views:

44

answers:

3

Is there a method, javascript script or anything that could allow to preload hyperlinks in the page? For instance, I want that when user comes to http://ahostel.lt/ and the page fully loads, the script would start preloading other pages in the navigation (Booking, Map, Facilities, Look around, [...]). So when user clicks on any of those hyperlinks, page would load instantaneously. How this can be done?

+2  A: 
Pointy
*cough* `this.href` *cough* :)
Nick Craver
No, no. jquery is welcome here. : ) Though would this work?
Guy
OK Nick - I'm sensing that you're on a mini-crusade :-)
Pointy
Make sure to use `var urls = $('a').map(function(i, a) { return a.href; }).get(0);` or `var urls = $('a').map(function() { return this.href; }).get(0);`, the first param is the index :)
Nick Craver
Thanks Nick - it always drives me nuts that ".map()" and ".each()" are different and generally I have to check api.jquery.com every time I type it in!!
Pointy
+1  A: 

Have not tried it, but based on this blog post, you could do the following:

$("a").each(function(){
  $.ajax({ url:$(this).attr("href"), cache:true, dataType:"text" });
});

You should be careful though if you have links like Logoff the user could get logged off. See Wikipedia for more problems with prefetching.

Adam
@adam that's a really good point
Pointy
Well, I want to allow clients to select which pages to prefetch, for instance with a selector $('a[prefetch="true"]'). The problem, however, is that it would attempt to prefetch all pages every time user goes to other page in the navigation.
Guy
Those issues with prefetching are all real, but most of them are moot if you're talking about prefetching your own pages from your own site. If the links are *not* to ones own site, then I totally agree that it's a pretty rude practice.
Pointy
@Guy unless we're talking about zillions of links here, a redundant prefetch is probably going to be pretty cheap because the browser's going to see that it's cached. Again, however, if there are too many links it could become a problem.
Pointy
@Guy assuming the browser has some caching those requests made to prefetch from the next page will be really fast or not made at all. This depends on the Cache headers your web server is putting out as well.
Adam
@Pointy. I agree if you are careful prefetching is OK on your own site. I made my last comment before I saw yours, sorry for the redundancy.
Adam
+1  A: 

There's actually provision in the HTML5 spec for this, though it's currently only supported by Firefox.

<link rel="next" href="page2.html">

Just throwing this to you as a non-javascript alternative.

Malabar Front
I think you also need to indicate prefetch attribute http://browserspy.dk/prefetch.php.
Guy