Get the full URI from the href property of a link

Posted by Savageman on Stack Overflow See other posts from Stack Overflow or by Savageman
Published on 2010-04-14T16:15:17Z Indexed on 2010/04/14 16:33 UTC
Read the original article Hit count: 373

Filed under:
|

Hello SO,

I would like to have a confirmation on some point.

My goal is to always get the same string (which is the URI in my case) while reading the href property from a link. Example:

<a href="test.htm" /> with base_url = http://domain.name/

<a href="../test.htm" /> with base_url = http://domain.name/domain/

<a href="http://domain.name/test.htm" /> with base_url = any folder from http://domain.name/

I need to get http://domain.name/test.htm from the 3 situations above (or any other identical string).

After some tests, it appears that my_a_dom_node.href always return the full-qualified URI, including the http://domaine.name, which should be okay for what I want.

jQuery has a different behaviour and $(my_a_dom_node).attr('href') returns the content (text) that appears inside the HTML. So my trick is to use $(my_a_dom_node).get(0).href to get the full URI.

The question is: can I rely on this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about link