You could set up flags depending on the window size, everything dimensions information you will need is in the snippet above.
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
Keep in mind that you may need to bind script loading on a resize event handler.
I highly recommend using a debouncing pipe function in order to handle resizing more efficiently.
Afterwards you can load scripts using some utility functions as:
function loadScript(url, callback) {
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
}
if (x > 1000) {
//desktop
loadScript("desktop.js", myPrettyCode);
} else {
//mobile tablet
loadScript("mobile.js", myPrettyCode);
}
Keep in mind that utility libraries like Modernizr have some useful feature detection helpers that may help you. David Walsh has also provided a quite smart way to detecting screen sizes.
Also , unloading a script is pretty harsh here is a relevant StackOverflow question
You may also take a look at this question as it may be helpful as well as taking a look at require.js , it's an efficient JavaScript file and module loader.