Skip to content

Instantly share code, notes, and snippets.

@necolas
Created June 14, 2011 20:36
Show Gist options
  • Select an option

  • Save necolas/1025811 to your computer and use it in GitHub Desktop.

Select an option

Save necolas/1025811 to your computer and use it in GitHub Desktop.

Revisions

  1. Nicolas Gallagher revised this gist Feb 20, 2012. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions snippet.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,19 @@
    /*
    * Updated to use the function-based method described in http://www.phpied.com/social-button-bffs/
    * but retaining the use of a document fragment to minimise the number of times objects are
    * written to the DOM. Also, better handling of scripts without supplied ids.
    * Better handling of scripts without supplied ids.
    *
    * N.B. Be sure to include Google Analytics's _gaq and Facebook's fbAsyncInit prior to this function.
    */

    (function(doc, script) {
    var js,
    fjs = doc.getElementsByTagName(script)[0],
    frag = doc.createDocumentFragment(),
    add = function(url, id) {
    if (doc.getElementById(id)) {return;}
    js = doc.createElement(script);
    js.src = url;
    id && (js.id = id);
    frag.appendChild( js );
    fjs.parentNode.insertBefore(js, fjs);
    };

    // Google Analytics
    @@ -25,7 +23,5 @@
    // Facebook SDK
    add('//connect.facebook.net/en_US/all.js', 'facebook-jssdk');
    // Twitter SDK
    add('//platform.twitter.com/widgets.js');

    fjs.parentNode.insertBefore(frag, fjs);
    add('//platform.twitter.com/widgets.js', 'twitter-wjs');
    }(document, 'script'));
  2. necolas revised this gist Nov 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion snippet.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    if (doc.getElementById(id)) {return;}
    js = doc.createElement(script);
    js.src = url;
    js.id = id || null;
    id && (js.id = id);
    frag.appendChild( js );
    };

  3. necolas revised this gist Oct 16, 2011. 2 changed files with 28 additions and 36 deletions.
    55 changes: 28 additions & 27 deletions snippet.js
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,31 @@
    ( function ( win, doc ) {
    /*
    * Updated to use the function-based method described in http://www.phpied.com/social-button-bffs/
    * but retaining the use of a document fragment to minimise the number of times objects are
    * written to the DOM. Also, better handling of scripts without supplied ids.
    *
    * N.B. Be sure to include Google Analytics's _gaq and Facebook's fbAsyncInit prior to this function.
    */

    // Google Analytics global variable
    win._gaq = [ ['_setAccount','UA-XXXXX-X'], ['_trackPageview'], ['_trackPageLoadTime'] ];
    (function(doc, script) {
    var js,
    fjs = doc.getElementsByTagName(script)[0],
    frag = doc.createDocumentFragment(),
    add = function(url, id) {
    if (doc.getElementById(id)) {return;}
    js = doc.createElement(script);
    js.src = url;
    js.id = id || null;
    frag.appendChild( js );
    };

    // Array of cross-domain script urls
    var urls = [
    '//connect.facebook.net/en_US/all.js', // Facebook SDK
    '//platform.twitter.com/widgets.js', // Twitter Widgets
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js' // Google Analytics
    ],
    // Google Analytics
    add(('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js', 'ga');
    // Google+ button
    add('https://apis.google.com/js/plusone.js');
    // Facebook SDK
    add('//connect.facebook.net/en_US/all.js', 'facebook-jssdk');
    // Twitter SDK
    add('//platform.twitter.com/widgets.js');

    length = urls.length,
    nodeName = 'script',
    fragment = doc.createDocumentFragment(), // create container for DOM nodes
    elem = doc.createElement( nodeName ), // create script element
    temp;

    while ( length -- ) {
    temp = elem.cloneNode( false );
    temp.src = urls[ length ];
    temp.async = true;
    fragment.appendChild( temp );
    }

    // insert documentFragment before first script element
    ( temp = doc.getElementsByTagName( nodeName )[0] ).parentNode.insertBefore( fragment, temp );

    } ( this, document ) );
    fjs.parentNode.insertBefore(frag, fjs);
    }(document, 'script'));
    9 changes: 0 additions & 9 deletions snippets.min.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    (function(w,d){
    w._gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview'],['_trackPageLoadTime']];
    var u=[
    '//connect.facebook.net/en_US/all.js', // Facebook SDK
    '//platform.twitter.com/widgets.js', // Twitter Widgets
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js' // Google Analytics
    ],i=u.length,n='script',f=d.createDocumentFragment(),e=d.createElement(n),t;while(i--){t=e.cloneNode(false);t.async=t.src=u[i];f.appendChild(t);}(t=d.getElementsByTagName(n)[0]).parentNode.insertBefore(f,t);
    }(this,document));
  4. necolas revised this gist Jun 15, 2011. 2 changed files with 29 additions and 17 deletions.
    37 changes: 20 additions & 17 deletions snippet.js
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,30 @@
    // Google Analytics variable
    var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview'],['_trackPageLoadTime']];
    ( function ( win, doc ) {

    // Cross-domain scripts
    (function(d,e){
    // Google Analytics global variable
    win._gaq = [ ['_setAccount','UA-XXXXX-X'], ['_trackPageview'], ['_trackPageLoadTime'] ];

    var u = [
    '//connect.facebook.net/en_US/all.js', // Facebook JS SDK
    // Array of cross-domain script urls
    var urls = [
    '//connect.facebook.net/en_US/all.js', // Facebook SDK
    '//platform.twitter.com/widgets.js', // Twitter Widgets
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    ('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js' // Google Analytics
    ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js' // Google Analytics
    ],

    i = u.length,
    f = d.createDocumentFragment(),
    s = d.createElement(e),
    t;
    length = urls.length,
    nodeName = 'script',
    fragment = doc.createDocumentFragment(), // create container for DOM nodes
    elem = doc.createElement( nodeName ), // create script element
    temp;

    while (i--) {
    t = s.cloneNode(false);
    t.async = t.src = u[i];
    f.appendChild(t);
    while ( length -- ) {
    temp = elem.cloneNode( false );
    temp.src = urls[ length ];
    temp.async = true;
    fragment.appendChild( temp );
    }

    (t = d.getElementsByTagName(e)[0]).parentNode.insertBefore(f, t);
    // insert documentFragment before first script element
    ( temp = doc.getElementsByTagName( nodeName )[0] ).parentNode.insertBefore( fragment, temp );

    }(document,'script'));
    } ( this, document ) );
    9 changes: 9 additions & 0 deletions snippets.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    (function(w,d){
    w._gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview'],['_trackPageLoadTime']];
    var u=[
    '//connect.facebook.net/en_US/all.js', // Facebook SDK
    '//platform.twitter.com/widgets.js', // Twitter Widgets
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js' // Google Analytics
    ],i=u.length,n='script',f=d.createDocumentFragment(),e=d.createElement(n),t;while(i--){t=e.cloneNode(false);t.async=t.src=u[i];f.appendChild(t);}(t=d.getElementsByTagName(n)[0]).parentNode.insertBefore(f,t);
    }(this,document));
  5. necolas revised this gist Jun 15, 2011. 1 changed file with 15 additions and 11 deletions.
    26 changes: 15 additions & 11 deletions snippet.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,27 @@
    // Google Analytics variable
    var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview'],['_trackPageLoadTime']];

    // Cross-domain scripts
    (function(d,e){

    var u = [
    ('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js', // Google Analytics
    '//connect.facebook.net/en_US/all.js', // Facebook JS SDK
    '//platform.twitter.com/widgets.js', // Twitter Widgets
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    '//connect.facebook.net/en_US/all.js' // Facebook JS SDK
    ('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js' // Google Analytics
    ],

    i = u.length,
    f = d.createDocumentFragment(),
    s = d.createElement(e),
    t = d.getElementsByTagName(e)[0],
    i = u.length,
    z;
    t;

    while(i--){
    z = s.cloneNode(true);
    z.src = u[i];
    z.async = 1;
    f.appendChild(z);
    while (i--) {
    t = s.cloneNode(false);
    t.async = t.src = u[i];
    f.appendChild(t);
    }

    t.parentNode.insertBefore(f,t);
    (t = d.getElementsByTagName(e)[0]).parentNode.insertBefore(f, t);

    }(document,'script'));
  6. necolas created this gist Jun 14, 2011.
    23 changes: 23 additions & 0 deletions snippet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    (function(d,e){

    var u = [
    ('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js', // Google Analytics
    'https://apis.google.com/js/plusone.js', // Google +1 Button
    '//connect.facebook.net/en_US/all.js' // Facebook JS SDK
    ],
    f = d.createDocumentFragment(),
    s = d.createElement(e),
    t = d.getElementsByTagName(e)[0],
    i = u.length,
    z;

    while(i--){
    z = s.cloneNode(true);
    z.src = u[i];
    z.async = 1;
    f.appendChild(z);
    }

    t.parentNode.insertBefore(f,t);

    }(document,'script'));