DMIL
DMIL

Reputation: 703

How to test if jQuery and certain other scripts have loaded?

I know about document.ready() but I don't want to wait for some content on external servers like Google Analytics, some ad serving content or anything else that isn't absolutely required for the website. I see this problem on sites that have user comments, and usually every single external piece of content has to be loaded before comments become available to use and even if one of the many CDNs is late, it blocks everything else.

Upvotes: 0

Views: 52

Answers (1)

netpoetica
netpoetica

Reputation: 3425

I don't think you need to test it as much as you need to defer it. Here is a really simple example from Google.

(the following code copied from Google)

 // Add a script element as a child of the body
 function downloadJSAtOnload() {
     var element = document.createElement("script");
     element.src = "deferredfunctions.js";
     document.body.appendChild(element);
 }

 // Check for browser support of event handling capability
 if (window.addEventListener)
     window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
     window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;

Essentially, add a new script tag at the documentReady state when you're executing your code already.

Upvotes: 1

Related Questions