25

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?

Savageman
  • 9,257
  • 6
  • 40
  • 50

4 Answers4

25

YES, you can rely!

Once when people was using simple javascript (no jQuery) many asked the opposite of what you are asking, they wanted to get the real url as written in the href attribute and not the full one, in such case they used to simply do:

my_a_dom_node.getAttribute('href', 2); //works both IE/FF

Then it came jQuery that helped people not to waste their time in finding out they would need such code and jQuery always returns the real url as written in the href attribute.

It's funny that now someone is asking how to get the full url because jQuery returns the one written in the href attribute.

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
  • Yes, I found the questions you are talking about when I wrote my question. Thank you for the answer. – Savageman Apr 14 '10 at 17:14
  • Why are you supplying a second argument of value `2`? `getAttribute` only has one argument. – Noitidart Jul 04 '14 at 20:32
  • @Noitidart: getAttribute comes with two arguments, the 2nd argument is optional: http://msdn.microsoft.com/en-us/library/ms536429(v=vs.85).aspx – Marco Demaio Jul 10 '14 at 12:40
  • Oh I see thanks man. I think that's IE only. FF docs: https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute?redirectlocale=en-US&redirectslug=DOM%2Felement.getAttribute – Noitidart Jul 10 '14 at 21:56
  • @Noitidart: yes it's IE only, but Firefox doesn't fire an error if you provide a second argument, it simply ignores it. – Marco Demaio Aug 08 '14 at 16:53
15

I know it's an old question, but this being actually the first entry that popped up, I believe it's good to add an extra solution. Ever since jQuery introduced the "prop" function, getting the full URL is as simple as:

$(my_a_dom_node).prop('href');

I hope that still helps somebody.

arakell
  • 151
  • 1
  • 2
10

Yes, you can rely on this.

Looking through the jQuery (1.4.2) source code, I see the jQuery.attr() function being used (condensed to the relevant parts):

jQuery.extend({
  // ...
  attr: function( elem, name, value, pass ) {
    // ...

    var attr = !jQuery.support.hrefNormalized && notxml && special ?
               // Some attributes require a special call on IE
               elem.getAttribute( name, 2 ) :
               elem.getAttribute( name );

    // Non-existent attributes return null, we normalize to undefined
    return attr === null ? undefined : attr;    
  }
});

So it´effectively calls elem.getAttribute('href') which returns the actual attribute value, while the href property by design returns the canonical URL.

However, there is a reference to jQuery.support.hrefNormalized, of which the jQuery support site has to say:

  • hrefNormalized: Is equal to true if the .getAttribute() method retrieves the href attribute of elements unchanged, rather than normalizing it to a fully-qualified URL. (It is currently false in IE, the URLs are normalized). DOM l3 spec

This basically means that jQuery finds out browser behavior on its own and adapts accordingly to provide a consistent result.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Great, thank you for the answer. I wish I could give 2 accepted anwser here. I voted Marco Demaio, because he was first and basically say the same. – Savageman Apr 14 '10 at 17:16
  • @Savageman: Yes… took me a while to dig through the source code. ;) – Tomalak Apr 14 '10 at 19:59
2

You could recompose the full url using a bit of javascript :

function parseLink(link) {
    var baseUrl = location.href.substring(0,location.href.lastIndexOf('/'));
    if (link.indexOf('/') != -1) {
        link = link.substring(link.lastIndexOf('/')); 
    } else {
        link = "/"+ link;
    }
    var fullUrl = baseUrl + link;
    return fullUrl
}
rnaud
  • 2,610
  • 32
  • 38
  • Would that really work for ANY sub-folder and relative path? From my understanding, it's not the case. – Savageman Apr 14 '10 at 16:35
  • I think so, the baseUrl thing is pretty straight forward. Maybe someone to prove me wrong ? – rnaud Apr 14 '10 at 16:37