I'm currently using the following function to 'convert' a relative URL to an absolute one:
function qualifyURL(url) {
var a = document.createElement('a');
a.href = url;
return a.href;
}
This works quite well in most browsers but IE6 insists on returning the relative URL still! It does the same if I use getAttribute('href').
The only way I've been able to get a qualified URL out of IE6 is to create an img element and query it's 'src' attribute - the problem with this is that it generates a server request; something I want to avoid.
So my question is: Is there any way to get a fully qualified URL in IE6 from a relative one (without a server request)?
Before you recommend a quick regex/string fix I assure you it's not that simple. Base elements + double period relative urls + a tonne of other potential variables really make it hell!
There must be a way to do it without having to create a mammoth of a regex'y solution??
This solution works in all browsers.
However, it doesn't support relative URLs with ".." in them, like "../file.png".
If
url
does not begin with '/'Take the current page's url, chop off everything past the last '/'; then append the relative url.
Else if
url
begins with '/'Take the current page's url and chop off everything to the right of the single '/'; then append the url.
Else if
url
starts with # or ?Take the current page's url and simply append
url
Hope it works for you
If it runs in the browser, this sort of works for me..
As long as the browser implements the <base> tag correctly, which browsers tend to:
Here's a jsfiddle where you can experiment with it: http://jsfiddle.net/ecmanaut/RHdnZ/
You can make it work on IE6 just cloning the element:
(Tested using IETester on IE6 and IE5.5 modes)
How strange! IE does, however, understand it when you use innerHTML instead of DOM methods.
A bit ugly, but more concise than Doing It Yourself.