Relative URL to an absolute URL?

J

John Nagle

Is there any standard way to convert a relative URL to an absolute URL?
Obviously the browser knows how to do this. But doing it in Javascript
requires getting the document URL, parsing it, looking for a BASE element,
parsing that, taking in the URL to be made absolute, parsing that,
and then handling all the special cases while reassembling the parts.

Python's URLparse module has all the machinery for this. It is
several hundred lines of code, and I recently had to fix a bug in it.
I'd prefer not to have to translate that to Javascript, especially
since it's not very efficient and I need to apply it to all the links
on a page.

Somebody must have done this, but I'm not finding anything useful with Google.

John Nagle
 
P

pr

John said:
Is there any standard way to convert a relative URL to an absolute URL?
Obviously the browser knows how to do this. But doing it in Javascript
requires getting the document URL, parsing it, looking for a BASE element,
parsing that, taking in the URL to be made absolute, parsing that,
and then handling all the special cases while reassembling the parts.

Python's URLparse module has all the machinery for this. It is
several hundred lines of code, and I recently had to fix a bug in it.
I'd prefer not to have to translate that to Javascript, especially
since it's not very efficient and I need to apply it to all the links
on a page.

If it's only links you're interested in, the solution may be as simple
as accessing linkElement.href instead of linkElement.getAttribute("href").

Otherwise, I recently wrote this function in a bookmarklet and would be
interested to know if any cases defeat it (aside from anchors, which it
doesn't handle) that I haven't thought of yet:

function toAbsURL(s) {
var l = location, h, p, f, i;
if (/^\w+:/.test(s)) {
return s;
}

h = l.protocol + '//' + l.host;
if (s.indexOf('/') == 0) {
return h + s;
}

p = l.pathname.replace(/\/[^\/]*$/, '');
f = s.match(/\.\.\//g);
if (f) {
s = s.substring(f.length * 3);
for (i = f.length; i--;) {
p = p.substring(0, p.lastIndexOf('/'));
}
}

return h + p + '/' + s;
}
 
S

SAM

John Nagle a écrit :
Is there any standard way to convert a relative URL to an absolute URL?
Obviously the browser knows how to do this. But doing it in Javascript

<html><!-- to test somewhere in the site -->
<a href="../folder1/file1.htm">test 1</a>
<a href="../folder2/file1.htm">test 2</a>
<a href="../../file1.htm">test 3</a>
<form action="repport.php" name="form1">
<input name="linker" type="hidden">
<input type=submit value="send absolute links">
</form>
<script type="text/javascript">
var A = document.links;
for(var i=0; i<A.length; i++)
if(A.href && A.href.length>1) {
A.title = A.href;
document.form1.linker.value += A.href+'|';
}
</script>
</html>
 
J

John Nagle

pr said:
If it's only links you're interested in, the solution may be as simple
as accessing linkElement.href instead of linkElement.getAttribute("href").

Thanks. I didn't realize that ".href" forced the conversion to an absolute
URL. I'd been using 'getAttribute("href")', because I'm writing for
Greasemonkey, which has some wrapper issues, but not for this item.

John Nagle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top