How to get "raw" href contents in JavaScript

wfaulk picture wfaulk · Oct 11, 2009 · Viewed 48.1k times · Source

I am trying to write a GreaseMonkey script in which I want to find all of the links that are relative links. It seemed to me that the way to do that would be to match the contents of href against /^https?:///.

But I find that when I access the anchor's href attribute, it's always normalized or cooked into a form that contains "http". That is, if the HTML contains:

<a id="rel" href="/relative/link">inner</a>

accessing

document.getElementById("rel").href

returns

http://example.com/relative/link

How can I access the raw data in the href attribute?

Alternately, is there a better way to find relative links?

Answer

Gumbo picture Gumbo · Oct 11, 2009

Try the getAttribute method instead.