-2

This happens in both firefox and chrome. I have a perfectly ordinary link, like so:

<a href='somewhere'>this is my link</a>

But, the browser is converting this into an absolute link, like this:

<a href='http://www.example.com/dir/page/somewhere'>this is my link</a>

How do I prevent browsers from doing this? It is causing problems, because I have javascript on the page that depends on the href attribute NOT having the http:// prefix, and when browsers do this is breaks my code. So, is there a way to prevent this from happening? Or, is there a way to access the original href attribute, the way it was before the browser modified it?

I am not using jquery. I do have prototype available, but I would prefer to know how to do this in pure javascript.

Benubird
  • 18,551
  • 27
  • 90
  • 141
  • Why not modify your script to simply remove the http:// or https:// if it is present? – Justin Niessner Mar 25 '13 at 17:25
  • I cannot reproduce this: http://jsfiddle.net/Btnbs/show/ (view source shows the original link) - using Chrome latest. – Danny Beckett Mar 25 '13 at 17:25
  • @DannyBeckett I went to that jsfiddle, and sure enough 'alert(document.getElementsByTagName('a')[0].href)` produces a full link, not just the "somewhere", both chrome and firefox – Benubird Mar 25 '13 at 17:29
  • Now you've made that clearer; you never mentioned any problematic JS before, you just said "the browser is converting this"... hence the 2 downvotes. I took it as the source code was changing. – Danny Beckett Mar 25 '13 at 17:31
  • @JustinNiessner Because the particular piece of code I'm working on is part of a template that will be included in sevaral places, and needs to be able to handle any links - e.g. it might be href='aaa/bbb/ccc' but get converted to 'http://example.com/xxx/yyy/aaa/bbb/ccc'. I'd need some sort of psychic regex to detect that :p – Benubird Mar 25 '13 at 17:32
  • 1
    *Exact duplicate:* [How to get "raw" href contents in JavaScript](http://stackoverflow.com/q/1550901) – Danny Beckett Mar 25 '13 at 17:33
  • @DannyBeckett No, a view source on the page still shows the right values, I just can't access them. I could be wrong about it being caused by the browser, but with noting else on the page (and no plugins or mods of the browser either) I can't think what else it could be. – Benubird Mar 25 '13 at 17:34
  • @DannyBeckett Thanks! I did search before asking, but didn't find the other question - thanks for pointing it out. – Benubird Mar 25 '13 at 17:36
  • No problem! (I actually found it via Google, whilst trying to find an answer for you ;) ) – Danny Beckett Mar 25 '13 at 17:38

1 Answers1

2

The attributes array on a DOM object holds the actual, original values for the attributes as posted in the page's HTML tags .. hence

console.log(object.attributes.href.value)
or
console.log(object.getAttribute('href')

Will do it ...

Radiotrib
  • 629
  • 5
  • 8