Conditionally Load a Javascript File

Conditionally load a one or several Javascript files. This technique could be useful for a game, where the user makes a choice and game modules load.  Also could be used to load different Javascript versions of the same page based on browser compatibility.

function loadScript(src) {
    var s = document.createElement('script');
    s.src = src + "?" + new Date().getTime();
    s.type = "text/javascript";
    s.async = false;


Place the function above in the head or at the bottom of the page.  Load the script file by calling the function, for example:

// some previous condition sets boolean version either true of false

if ( version ) {
} else {

Copyright © Website Development | Disclaimer | Privacy Policy | Terms of Use