Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preload a page using HTML5?

Tags:

html

preload

I remember reading about a meta tag that makes the browser preload a page. What's the tag again?

like image 205
Leo Jiang Avatar asked Oct 20 '11 03:10

Leo Jiang


People also ask

How do you preload a page in HTML?

In the header you have to add <link rel="preconnect" href="https://example.com/"> Prefetch downloads the resource and stores it in the browser cache to use it later. You can do <link rel="prefetch" href="imgs/image.

How do I add preload to my website?

In Google Chrome right click anywhere on the page and click on inspect element, this will bring up the developer tools. Right click on the body element and add a new attribute class="loaded" . Hit enter and you'll see our preloader screen disappear.

What does preload mean in HTML?

The preload attribute specifies if and how the author thinks that the media file should be loaded when the page loads. The preload attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience.

How do I preload an image in HTML?

To preload responsive images, new attributes were recently added to the <link> element: imagesrcset and imagesizes . They are used with <link rel="preload"> and match the srcset and sizes syntax used in <img> element.


2 Answers

Prefetching is included in a W3C spec under the name Resource Hints. It is implemented in Firefox, Chrome, IE 11, Edge, Opera after 12.1, and the Android Browser from 4.4.4, see the caniuse prefetch page for more and up-to-date details.

Also see the caniuse and spec pages for related technologies (supported browsers afterwards are retrieved from caniuse and up-to-date as of September 2015):

  • Prerendering caniuse / spec (IE 11, Edge, Chrome, Opera)
  • Preconnecting caniuse / spec (Firefox, Chrome 46, Opera 33)
  • DNS Prefetching caniuse / spec (IE9 (see note below), IE10, every other browser except Opera Mini and perhaps iOS Safari and the Android Browser)

IE 9 implemented DNS prefetching only but called it "prefetch" (caution!). Chrome for a while (at least as far as 2013) only did prerendering and DNS prefetching. IE11 implements lazyload, for images; Microsoft has tried to get it in the spec but so far it isn't. iCab is stated to have been the first browser to implement prefetching, although this behaviour was automatic, not controlled by the markup.


Historical background

The Mozilla Application Suite, and later, Firefox, implement the spec (the spec is actually based on Mozilla's early implementation of prefetching, which was somewhat based on the Link: header specified in RFC 2068 which has now been superseeded by RFC 2616 [which does not reference the Link: header]. See this old version of the docs (🕔) for more detail.) As per the documentation on MDN (🕔):

Link prefetching is a browser mechanism, which utilizes browser idle time to download or prefetch documents that the user might visit in the near future.

The browser looks for either an HTML <link> or an HTTP Link: header with a relation type of either next or prefetch.

So the syntax is:

<link rel="prefetch" href="/path/to/prefetch" /> 

You can also use the Link: HTTP header:

Link: </page/to/prefetch>; rel=prefetch 

Or a <meta> to simulate that same HTTP header:

<meta http-equiv="Link" content="&lt;/page/to/prefetch&gt;; rel=prefetch"> 

Note that the next relation can also be used, but its main function is to indicate the "next" page in the navigation, so you should not use it for resources or unrelated information. Prefetching is also performed on HTTPS connections.

iCab

iCab seems to (🕔) have implemented an early prefetching around 2001. iCab apparently prefetched all links to content pages (not resources), not following any hint the developer would have left in the markup.

like image 136
16 revs, 2 users 87% Avatar answered Sep 20 '22 01:09

16 revs, 2 users 87%


Some user agents may choose to preload when this is present, but you can't bet on it.

<link rel="next" href="http://www.example.com/link-reference"> 
like image 27
Syntax Error Avatar answered Sep 23 '22 01:09

Syntax Error