jeremy-h of Web Development
11/1/2017 - 6:48 PM

Defer Scripts

Scripts that assist in deferring the loading of external scripts. If editing needs to occur in the DOM element that calls the script that will be listed in the comments.

// Defer Iframe Loading - move src to data-src and leave src empty
function init() {
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
if(vidDefer[i].getAttribute('data-src')) {
vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
} } }
window.onload = init;

// Defer sripts by placing their calls in an internal file
// Place this script immediately before the </body>
// Keep defer.js file in root
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "defer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

// Defer Script Files only - requires jQuery
$(window).load(function(){
    $.getScript("//apis.google.com/js/platform.js");
    $.getScript("//platform.linkedin.com/in.js")
});