MyBB Community Forums

Full Version: How to Asynchronously Load Javascript?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm attempting to asynchronously load my javascript files to speed up load times, but nothing I've tried seems to work correctly.

For example This bit of JS:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 

Doing a bit of research, I found that to load the JS asynchronously it should look something like this:

(function(){
	    var a = document.createElement('script');
	    a.type = 'text/javascript';
	    a.async = true;
	    a.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
            var b = document.getElementsByTagName('script')[0];
	    b.parentNode.insertBefore(a, b);
	})();

However, the above method does not work. I've already searched the forum and other alternative resources and can't seem to find any information on forcing the asynchronous loading of javascript.

Thanks in advance!