Last active
March 16, 2019 16:30
-
-
Save samperrow/811ab1ff9548b38583c886e7dbbed53e to your computer and use it in GitHub Desktop.
This script will load multiple JS files dynamically without jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExecuteOrDelayUntilScriptLoaded(loadJS, 'sp.js'); | |
function loadJS() { | |
var index = 0; | |
var site = document.location.href; | |
var resources = []; | |
function createResource(type, location, url, code) { | |
var obj = document.createElement(type); | |
if (type === 'script') { | |
if (url) obj.src = url; | |
if (code) obj.text = code; | |
} else { | |
obj.href = url; | |
obj.rel = 'stylesheet'; | |
} | |
obj.location = location; | |
obj.onload = indexCount; | |
return obj; | |
} | |
function sendScript(index) { | |
return resources[index].location.appendChild(resources[index]); | |
} | |
function indexCount() { | |
if (index < resources.length) { | |
sendScript(index); | |
index++; | |
} | |
} | |
// deps | |
resources.push(createResource('link', document.head, '/assets/code/css/dataTables-1.10.18.min.css', null)); | |
resources.push(createResource('script', document.body, '/assets/code/js/libs/jquery.dataTables-1.10.19.min.js', null)); | |
resources.push(createResource('script', document.body, '/assets/code/js/libs/spcalendarpro.js', null)); | |
if (/Pages\/Intro.html/i.test(site)) { | |
resources.push(createResource('script', document.body, '/assets/intro-dep.js', null)); | |
resources.push(createResource('script', document.body, '/assets/intro.js', null)); | |
} | |
else if (/Pages\/Home.html/i.test(site)) { | |
resources.push(createResource('script', document.body, '/assets/home-dep.js', null)); | |
resources.push(createResource('script', document.body, '/assets/home.js', null)); | |
} | |
else if (/Pages\/Welcome.html/i.test(site)) { | |
resources.push(createResource('script', document.body, '/assets/js/encounters.js', null)); | |
resources.push(createResource('script', document.body, '/assets/js/search.js', null)); | |
} | |
indexCount(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment