开发者

preload="true" hyperlinks

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 开发者_Go百科(Booking, Map, Facilities, Look around, [...]). So when user clicks on any of those hyperlinks, page would load instantaneously. How this can be done?


You could do something like this:

$(function() {
  $('a').each(function() {
    new Image().src = $(this).attr('href'); // or just this.href
  });
});

They're not images, but the browser doesn't know that. You might want to slow that down a little so as not to choke the connection too much:

$(function() {
  var urls = $('a').map(function(_, a) { return a.href; }).get(0);
  var i = 0;
  function fetchOne() {
    if (i === urls.length) return;
    var img = new Image();
    img.load = fetchOne;
    img.src = urls[i++];
  };
  fetchOne();
});

edit — @Adam makes a really good point about "dangerous" links to fetch. It might be better to have some links marked as "noPrefetch" (as a class or something), or alternatively to only prefetch links marked explicitly with the class "prefetch".


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜