Loading CSS Asynchronously

Source: Internet
Author: User

When it comes to loading CSS, isn't that a simple thing? Like this:

<rel= "stylesheet"  href= "Cssfile.css">

This is not the finished child!

That's right! But that's a problem-it's blocking the rendering! browsers see this tag will stop the work, to load CSS and parse.

Of course, if this CSS file is the next to render the content required, that is understandable, must first have this CSS to continue to render, blocking is also reasonable.

However, the actual situation is that many of our CSS content in the first screen is not necessary, at least until the follow-up will be used, then this time these CSS loaded to block content rendering is not right.

In other words, for a less-than-high-priority (temporary) CSS, you should try to get it to load asynchronously, not blocking browser rendering.

So how do you do it?

Old prescription

Now introduce the first old-fashioned way: Insert the link tag asynchronously into the CSS code by JS, just like this:

var mycss = document.createelement ("link"= "stylesheet"= "MyStyles.css"-1]. nextSibling);

This is good to understand, is the subsequent JS execution, to create a link tag to load the CSS code.

Another way to do this is to use the property on link to media set it to a value that does not match the user's current browser environment, such as: media="print" , or even to a completely invalid value media="jscourse" .

As a result, the browser will assume that the CSS file has a very low priority and will load without blocking. But in order for the CSS rules to take effect, it is still necessary to change the media value to the correct line. So, this approach is implemented in code like this:

<rel= "stylesheet"  href= "Cssfile.css"  media= " Jscourse "  onload=" this.media= ' All ' ">

After introducing the old prescription, we'll take a look at the new prescription.

New Asynchronous Load Mode

The new recipe is to take advantage of the new additions in the specification rel="preload" , like this:

<rel= "preload"  href= "Cssfile.css"  as= "Style"  onload= "this.rel= ' stylesheet '">

Through the preload property value is to tell the browser this resource file will be used later, please load it well in advance. But this is just loading, so you see when it's loaded, you still need to change the REL value back, which will allow the CSS to take effect.

Do you want to ask: this and the old prescription also not much difference!

It certainly does, but it's more semantically better. In addition, you will find as="style" this attribute carefully, so preload can be used not only on CSS files, but also on most resource files. For example: JS file

<rel= "preload"  href= "Scriptfile.js"  as= "script ">

Then to use it, create a script tag to point to it:

var script = document.createelement ("script"), script.src = "Scriptfile.js";d ocument.body.appendChild (script);

This time the browser directly from the cache to take this file, no more requests, because it has been loaded.

So what are the resource files supported by the As property in preload? Here are some of the things you can do:

    • Audio
    • Document
    • Embed
    • Fetch
    • Font
    • Image
    • Object
    • Script
    • Style
    • Track
    • Worker
    • Video

Are you impatient to try? Tell you the bad news, currently preload only Chrome is the perfect support, other browsers miserable, hey!

Loading CSS Asynchronously

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.