Conditionally load JavaScript file

html, javascript

Solution

You'd have to create that markup yourself in JS. Something like this:

var head = document.getElementsByTagName('head')[0];
var js = document.createElement("script");

js.type = "text/javascript";

if (screen.width > 500)
{
    js.src = "js/jquery_computer.js";
}
else
{
    js.src = "js/mobile_version.js";
}

head.appendChild(js);

Problem

I need a JS statement that determine which JavaScript file to use. I have one file: ``` <script type="text/javascript" src="js/jquery_computer.js"></script> ``` But when the screen width is less than 500px, I want load another file instead: ``` <script type="text/javascript" src="js/mobile_version.js"></script> ``` I have tried everything and it is not working.

Original source

Related problems