简体   繁体   中英

How can I make my css files asynchronous?

I can async jss:

<script type="text/javascript">
    (function() {
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'http://www.gbilet.com/bundles/js?v=Yur37nohu3520qcbUHF9UVZYqsXFf5BzamwK0bJ0zjE1';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);
    })();
</script>

How can i just like it cs ??

The <script> tag supports the async attribute to perform async loading (otherwise all scripts will block the page rendering until they fully loaded)

The <style> tag doesn't support the async attribute at all, but you can emulate it using the syntax below on each CSS stylesheet inclusion

<link rel="preload" href="mystyles.css" as="style" onload="this.rel='stylesheet'">      
// create a link tag
var link = document.createElement( "link" );
link.rel = "stylesheet";
link.href = "main.css";

And then call a async function that inserts this link tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM