I am using the following javascript on a site within the <head> section:
<script type="text/javascript">
//Establish web analytics and call setup file
wa = new Object;
wa.currentURL = location.pathname;
wa.currentDomain = location.hostname;
if(wa.currentDomain.indexOf('cde') != -1 || wa.currentDomain.indexOf('test') != -1 || wa.currentDomain.indexOf('staging') != -1) {
wa.environment = 'dev';
wa.filePath = '//' + wa.currentDomain + '/scripts/webAnalytics/';
} else {
wa.environment = 'prod';
wa.filePath = '//www.mydomain.com/scripts/webAnalytics/';
}
//Function to include other js files
function addJavascript(jsname,pos) {
var th = document.getElementsByTagName(pos)[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',jsname);
th.appendChild(s);
}
//Use the function to call the web analytics setup file
addJavascript(location.protocol + wa.filePath + 'wa_SETUP.js','head');
</script>
This part seems to work just fine but I am also calling two other js files from within my wa_SETUP.js file using the following logic:
//Set the site variable & call the appropriate custom code file
if(wa.currentURL.indexOf("/blahblah/") != -1) {
wa.site = 'Blah';
addJavascript(location.protocol + wa.filePath + 'Blah.js','body');
} else if(wa.currentURL.indexOf("/whatever/") != -1) {
wa.site = 'Whatever';
addJavascript(location.protocol + wa.filePath + 'Whatever.js','body');
} else {
wa.site = 'Site Undefined';
}
addJavascript(location.protocol + wa.filePath + 'file3.js','body');
In my actual code I have more else if lines but the above shows my general strategy. Now, when I test this out using Fiddler I can see that all of my files are loading BUT any variables I have set within Blah.js or Whatever.js are not available for use in file3.js (the last file to be called). Any ideas on how I can ensure variables from Blah.js or Whatever.js are available to file3.js? Can I control load order/time somehow and make sure the previous files load before file3.js does?
Thanks!