If you are reading this, please get modern browser.
skip to main content | skip to main navigation | skip to secondary content

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

~ 29th August 2005. · 16:19 CET · permanent link · printer friendly ~

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 Comments

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

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

Sorry, the comment form is closed at this time, but if you have anything to say, please send me a message.

* Please keep in mind that this is a personal web site and it does not reflect the position or opinion of my respective employers, organizations or partners.

Typetester – compare screen type Supported by Veer.

What is this?

A web log of Marko Dugonjić, web professional from Croatia. Topics covered:

Translate this site

German, Spanish, Italian, French or Japanese (via).

See you there!

Feel like buying a book?

Try with maratz.com aStore

Worth visiting

top of the page | skip to main content | skip to main navigation | skip to secondary content