IE and Opera will return the absolute url for a src or href attribute of an element, even if it is coded relatively in the html or applied with a script. Firefox doesn't- it returns the literal relative text. I don't know what safari does.
Just knowing that there are differences, makes me always convert any path string I need to compare to another to an absolute path, so I don't have to remember which does what.
Added to that, if you want your code to work on a filesystem, either on a cd or a folder on your hard drive, you have to do flips to get the different syntaxes the browsers use for local urls. And if you have a subdomain it is handy to set a temporary site root folder for all those relative urls.
This may be overkill, but maybe you can use some piece of it:
- document.getRoot= function(rootStr){
-
var L= location.href;
-
L= L.replace(/\\/g,'/');
-
var ax= rootStr? L.indexOf(rootStr): -1;
-
ax= (ax== -1)? L.indexOf(location.pathname)+ 1: ax+ rootStr.length;
-
return L.substring(0,ax);
-
}
-
document.getPath=function(str){
-
var str= str.replace(/\\/g,'/');
-
if(str.search(location.protocol)==0 || /^(mailto|http)\:/i.test(str)){
-
return str;
-
}
-
var R=document.getRoot();
-
if(/^\//.test(str)) return R + str.substring(1);
-
-
var Loc= location.href.replace(/\\/g,'/');
-
var ax= Loc.lastIndexOf('/');
-
Loc= Loc.substring(0,ax);
-
var L= R.length;
-
-
while (str.search('../')==0){
-
if(ax<L) return '';
-
ax= Loc.lastIndexOf('/');
-
Loc= Loc.substring(0, ax);
-
str= str.substring(3);
-
}
-
return Loc + '/' + str;
-
}