Javascript Event for "Open in new Tab"

Jacek Francuz picture Jacek Francuz · Sep 11, 2012 · Viewed 11.5k times · Source

I have the following problem: I use Javascript onclick event to change href of a link. It works like a charm but only if user just clicks a link. If "Open in new tab" feature is used for the link - onclick event will not fire and href will never change. Is there any way to handle such an event? Perhaps with jQuery or some other JS Framework?

Example:

<a href="some_url" onclick="this.href = 'some_other_url'">Link</a>

Answer

Mateusz Rogulski picture Mateusz Rogulski · Sep 11, 2012

Try to change

<a href="some_url" onclick="this.href = 'some_other_url'">Link</a>

to

<a href="some_url" onmousedown="this.href = 'some_other_url'">Link</a>