Is there any recommended solutions when an embedding a script that will make a callback to the originating source but the js resides in a different domain. Sometimes other clients would be in a different domain and I can't rely on getting the document or window location.
Currently, I've been using this but not too happy with this solution.
var host = window.location.hostname;
if(host.indexOf('.devols.') > -1){
return (('https:' == document.location.protocol) ? 'https://' : 'http://') + host; // return dev or localhost
} else if(host.indexOf('.qaols.') > -1){
return 'qa environment';
} else {
return 'prod environment';
}
Update:
So far I've been playing around with this:
var scriptLocation = '';
var SCRIPT_NAME = 'example.js';
var scripts = document.getElementsByTagName('script');
for(var i=0;i<scripts.length;i++){
var src = scripts[i].getAttribute('src');
if(src){
var index = src.lastIndexOf(SCRIPT_NAME);
if((index > -1) && (index + SCRIPT_NAME.length == src.length)) {
scriptLocation=src.slice(0, src.indexOf('/my_script_location/'));
break;
}
}
}
return scriptLocation;
So after the page loads the code cycles thru the script tag array and determines the location so the api can make a callback without having to figure out the urls and etc...