main content, site navigation, search

The Difference Between href
and getAttribute(’href’) in JavaScript

For the HTML code <a id="aboutLink" href="/about/">about</a>, the document.getElementBy Id('aboutLink').href will return the full URL of the linked resource, for example http://somesite.com/about/.

If you want to get the exact value of the href attribute, you’d use document.getElementById('aboutLink').getAttribute('href'), which returns /about/. Yea, right – not in IE and Opera (both Win and Mac). See demo.

2 shouts to “The Difference Between href
and getAttribute(’href’) in JavaScript”

  1. Justin P
    001—2005.08.29.18:54

    I’ve been frustrated with IE’s lack for supporting that very handy method of retrieving element attributes for a long time, frustrating.

  2. endryou
    002—2005.09.05.03:46

    Check out the solution posted here:
    link to quirksmode.org. This saved my life.

Comments are closed.

main content, site navigation, search