Get the base URL of the forum in your JS
After looking at scriptaculous.js, I think I found a way to avoid relative path in the JS of our extensions:
I tested it in IE7, FF2 and Opera and it seems to work
var Vanilla = new function(){
//get the path of vanilla by looking in the src of the link that load global.js
//(got the hack from scriptaculous)
var scriptTags = document.getElementsByTagName("script");
for(var i=0;i<scriptTags.length;i++) {
if(scriptTags[i].src && scriptTags[i].src.match(/js\/global\.js$/)) {
this.baseURL = scriptTags[i].src.replace(/js\/global\.js$/,'');
break;
}
}
this.webRoot = this.baseURL.replace(/^http(s)?:\/\/[^\/]+/, '');
this.httpMethod = document.URL.replace(/^(http|https)(:\/\/).*$/, "$1$2");
if (this.webRoot == this.baseURL){
this.baseURL = this.httpMethod+document.domain+this.webRoot;
}
return this;
};
alert(Vanilla.baseURL+', '+Vanilla.webRoot)
I tested it in IE7, FF2 and Opera and it seems to work
0
This discussion has been closed.
Comments
var Lussumo = {}; Lussumo.PathFinder = function(jsPath){ var params = new function(){ var scriptTags = document.getElementsByTagName("script"); var src = ''; for(var i=0;i<scriptTags.length;i++) { if(scriptTags[i].getAttribute){ src = scriptTags[i].getAttribute('src'); } else { src = scriptTags[i].src } if(src.match(jsPath)){ break; } } this.scriptSrc = src; this.URL = document.URL; this.domain = document.domain; return this; }; if(params.scriptSrc){ this.webRoot = params.scriptSrc.replace(jsPath, ''); this.webRoot = this.webRoot.replace(/^http(s)?:\/\/[^\/]+/, ''); //because the src attr could have been a partial or complete url this.httpMethod = params.URL.replace(/^(http|https)(:\/\/).*$/, "$1$2"); this.baseURL = this.httpMethod + params.domain + this.webRoot; return this; } else { return nil; } }; var Vanilla = new Lussumo.PathFinder('js/global.js'); alert(Vanilla.webRoot+', '+Vanilla.baseURL+', '+Vanilla.httpMethod);
@ [-Stash-]: It get the src attribute of a link tag (in this example, js/global.js).
js addresses are always raw addresses in the core. So it should work what ever mode you are using.
And it should work whatever domain or http method you are using; from the src attribute http(s)://domain/path/to/vanilla/js/global.js or /path/to/vanilla/js/global.js, it will take 'js/global.js' to only keep the base URL or the web root.
If it works in safari that could be added to global.js or vanilla.js.
var Lussumo = {}; Lussumo.PathFinder = function(){ var params = new function(){ this.URL = document.URL; this.domain = document.domain; return this; }; this.getRootPath = function(tag, attr, path){ var Tags = document.getElementsByTagName(tag); var src = ''; var root = ''; for(var i=0;i<Tags.length;i++) { src = ''; if(Tags[i].getAttribute && Tags[i].getAttribute(attr)){ src = Tags[i].getAttribute(attr); } else if (eval("Tags["+i+"]."+attr)) { src = eval("Tags["+i+"]."+attr); } if(src.match(path)){ root = src.replace(path, ''); root = root.replace(/^http(s)?:\/\/[^\/]+/, ''); //because the src attr could have been a partial or complete url break; } } if(root){ return root; } else { return false; } }; this.webRoot = this.getRootPath('script', 'src', 'js/global.js'); this.styleUrl = this.getRootPath('link', 'href', 'vanilla.css'); this.httpMethod = params.URL.replace(/^(http|https)(:\/\/).*$/, "$1$2"); this.baseURL = this.httpMethod + params.domain + this.webRoot; return this; }; var Vanilla = new Lussumo.PathFinder(); alert(Vanilla.baseURL+', '+Vanilla.httpMethod+', '+Vanilla.styleUrl+', '+Vanilla.webRoot);
@ [-Stash-]: If Mark add it to the core, you will just need something like:
TB_LoadingAnimation = Vanilla.webRoot+'path/to/LoadingAnimation.gif';
if(!jQuery().PathFinder){ jQuery.PathFinder = function(){ var it = {}; var params = new function(){ this.URL = document.URL; this.domain = document.domain; return this; }; it.getRootPath = function(tag, attr, path){ var root = jQuery(tag+"[@"+attr+"$="+path+']').attr(attr); root = root.replace(path, ''); root = root.replace(/^http(s)?:\/\/[^\/]+/, ''); //because the src attr could have been a partial or complete url if(root){ return root; } else { return false; } }; it.webRoot = it.getRootPath('script', 'src', 'js/global.js'); it.styleUrl = it.getRootPath('link', 'href', 'vanilla.css'); it.httpMethod = params.URL.replace(/^(http|https)(:\/\/).*$/, "$1$2"); it.baseURL = it.httpMethod + params.domain + it.webRoot; return it; }; } if (!window.Vanilla){ var Vanilla = new jQuery.PathFinder(); }
if (!window.Vanilla){ var Vanilla = new PathFinder(); Vanilla.webRoot = Vanilla.getRootPath('script', 'src', 'js/global.js'); Vanilla.styleUrl = Vanilla.getRootPath('link', 'href', 'vanilla.css'); Vanilla.baseURL = Vanilla.params.httpMethod + Vanilla.params.domain + Vanilla.webRoot; }
if(!window.Vanilla)
(I edited the previous post)
both cannot coexist.
I think relative path should be avoided since you would have to help your addon users to set the rewrite rules.