views:

71

answers:

4

To hide the true destination of a link I'm using the following code:

<a href="http://example.com" onclick="window.location='http://url-i-want-to-hide.com/'; return false;">Click me</a>

However, if I do right-click and then open in new window, it goes to the url specified in the anchor href tag. I want it to go to the url specified in the javascript.

Is there a way to do this?

Thanks in advance

+1  A: 

You can't do this. Right click uses the true anchor, whereas you are overriding the click functionality of the anchor with javascript. A bit deceptive aren't we?

Drew
Google do the same thing to count outgoing hits
Pekka
Yes it is deceptive, and that is clearly the point. Google *does* do this on their search results. Go ahead, do a search and hover over a result. See the URL in the status bar? Now right click the link, copy shortcut, and paste it in address bar. Whoa!
Josh Stodola
+2  A: 

However, if I do right-click and then open in new window, it goes to the url specified in the anchor href tag. I want it to go to the url specified in the javascript.

No, but you could use JS to set the link href to the hidden URL after the document has loaded (or in the onfocus and onhover events of the link, although that feels rather kludgy and incomplete).

<a onfocus="this.href='hidden_url'" .....>

this is the way Google use to count outgoing links from the search results page.

Pekka
could you post the entire code for your examples, as I don't quite follow. Thanks
Steven
@Steven what is unclear? This is off the top of my head, so I don't have full examples ready
Pekka
your example uses onfocus to show the hidden url, which is the opposite of what I want. Anyhow it doesn't work in firefox. But what you said first is interesting. How would I do that? Thanks
Steven
@Steven nope, what I said first won't in fact work, you will see the hidden URL when hovering. The focus/click solution will work somehow (as I said, Google do this to hide their counter) but probably comes with some quirks I don't know more about
Pekka
@Pekka also add onblur handler to restore url
vittore
A: 

EDIT: Maybe I wasn't clear...sorry. Using an "#" would prevent them opening a different page in a new window. This may not work for your situation, and I don't know if you are trying to get something accomplished with SEO by hidding the URL. My solution is virtually the same as what you are currently doing except for the "#" in the href and I just prefer to have the window.location wrapped in a function....personal preference.

<a href="#" onclick="SendToUrl(); return false;"">Click Here</a>


<script language="javascript">
   function SendToUrl(){
       window.location='http://url-i-want-to-hide.com/'
    }
</script>
AGoodDisplayName
Yeah, this is what he has and what doesn't work.
Pekka
Are you saying your code won't solve the problem?
Steven
@Pekka: Updated answer. @Steven: I guess I am not sure why you are trying to do what you are trying accomplish with the way you are linking. Once you send them to the address you are trying to hide won't it just show up in the address bar? The only reason I could think of to do this was to maybe try to manipulate search relevance (SEO).
AGoodDisplayName
+1  A: 

All you need is a mousedown handler to fake them out...

<a href="http://example.com"
   onmousedown="this.href2 = this.href;
                this.href = 'http://url-i-want-to-hide.com/';"
   onmouseout="if(this.href2) this.href = this.href2;">Test</a>

And here is a live demo of it working.

Josh Stodola
i would also add `onblur` event to restore original url, because otherwise after click url will no longer be hidden
vittore
@vittore Good point
Josh Stodola
how would I create onblur event? (I'm a javascript novice)
Steven
@Steven answer updated to include `onblur` handler
Josh Stodola
@Steven @vittore Actually `onblur` is not a valid event for the anchor element. We are looking for `onmouseout`. Answer updated again, and I updated the live demo as well.
Josh Stodola
@josh I tried that but when I do a right click it shows the url I want hidden
Steven
@Steve Dude, this is as good as it gets! They're going to see the address when the page comes up anyways.
Josh Stodola
@Steve Plus, this answers the question you asked. When they choose open in new window, it does what you want it to do. If I were you I would stop trying to hide the URL because it simply can't be done to the extent you want to do it.
Josh Stodola
I added onfocus event and it hides the address in ie but not in firefox. If anyone can suggest something for firefox that would be great
Steven