Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Created March 12, 2013 09:59
Show Gist options
  • Save jasdeepkhalsa/5141681 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/5141681 to your computer and use it in GitHub Desktop.
Friendly iFrames (FIF) - Loading JavaScript Asynchronously Without Blocking window.onload by Stoyan Stefanov. Designed for loading third party scripts only (for first party scripts this script may have a negative performance impact, as tested by Yahoo)
// Documented by Stoyan Stefanov: https://www.facebook.com/note.php?note_id=10151176218703920
(function() {
var url = 'http://example.org/js.js';
var iframe = document.createElement('iframe');
(iframe.frameElement || iframe).style.cssText =
"width: 0; height: 0; border: 0";
iframe.src = "javascript:false";
var where = document.getElementsByTagName('script')[0];
where.parentNode.insertBefore(iframe, where);
var doc = iframe.contentWindow.document;
doc.open().write('<body onload="'+
'var js = document.createElement(\'script\');'+
'js.src = \''+ url +'\';'+
'document.body.appendChild(js);">');
doc.close();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment