SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    cache script syn or asyn ?

    Hi,
    I've a doubt
    First of all I know that's the asyn way no freeze the browser
    what's the best way for include script in a page
    for caching purpose syn or asyn ?
    syn
    PHP Code:
    <script src="//connect.facebook.net/en_US/all.js"></script> 
    asyn
    PHP Code:
    (function(d){
        var 
    jsid 'facebook-jssdk'ref d.getElementsByTagName('script')[0];
        if (
    d.getElementById(id)) {return;}
        
    js d.createElement('script');js.id id;js.async true;
        
    js.src '//connect.facebook.net/en_US/all.js';
        
    ref.parentNode.insertBefore(jsref);
    }(
    document)); 
    I think it's quite irrelevant but ........
    Thanks in advance

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,095
    Mentioned
    28 Post(s)
    Tagged
    1 Thread(s)
    The async attribute instructs browsers that understand it to proceed downloading the rest of the page and scripts while it's loading that script.
    It looks to have pretty complete support these days.

    I think you can always add it safely if the scripts aren't dependent on one another.
    I don't believe there's a difference between adding the async attribute to a tag or adding the script dynamically like in your example.

    I've never bothered worrying about micro optimisations like these.
    My scripts are always 1 or two scripts at the bottom so there's little need to download other things asynchronously.
    HTML Code:
    <script src="my-minified-compressed-js.js"></script>
    <script src="facebook.js"></script>
    <script src="google-analytics.js"></script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •