Relative URL to an absolute URL?

Discussion in 'Javascript' started by John Nagle, Feb 26, 2008.

  1. John Nagle

    John Nagle Guest

    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
     
    John Nagle, Feb 26, 2008
    #1
    1. Advertising

  2. John Nagle

    pr Guest

    John Nagle wrote:
    > 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;
    }
     
    pr, Feb 26, 2008
    #2
    1. Advertising

  3. John Nagle

    SAM Guest

    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>
     
    SAM, Feb 26, 2008
    #3
  4. John Nagle

    John Nagle Guest

    pr wrote:
    > John Nagle wrote:
    >> 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").


    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
     
    John Nagle, Feb 26, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wl
    Replies:
    1
    Views:
    4,479
  2. Andreas Zita

    Absolute url from relative path

    Andreas Zita, Dec 31, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    29,682
    bharathmr
    Apr 29, 2008
  3. =?Utf-8?B?UGF1bCBCb25mYW50aQ==?=

    Response.Redirect() converts relative URL to absolute URL

    =?Utf-8?B?UGF1bCBCb25mYW50aQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    6,313
    bruce barker \(sqlwork.com\)
    Apr 25, 2006
  4. Thomas Guettler
    Replies:
    3
    Views:
    791
    Andrei
    Oct 27, 2003
  5. Rudi Ahlers
    Replies:
    11
    Views:
    863
    Aaron Bertrand [MVP]
    Apr 11, 2004
Loading...

Share This Page