Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an absolute URL from a relative one. (IE6 issue)

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??

like image 422
James Avatar asked Jan 22 '09 21:01

James


People also ask

What does an absolute URL have that a relative URL does not?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point.

How do you make an absolute URL?

If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com ).

How do you find relative and absolute hyperlinks?

The main difference between absolute and relative paths is that absolute URLs always include the domain name of the site with http://www. Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain.


1 Answers

How strange! IE does, however, understand it when you use innerHTML instead of DOM methods.

function escapeHTML(s) {     return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;'); } function qualifyURL(url) {     var el= document.createElement('div');     el.innerHTML= '<a href="'+escapeHTML(url)+'">x</a>';     return el.firstChild.href; } 

A bit ugly, but more concise than Doing It Yourself.

like image 114
bobince Avatar answered Sep 30 '22 21:09

bobince