When i click a link, i want to execute a javascript function to get the dynamic url.
So i did like below.
<a href="javascript:myredirect()"> a link </a>
With this code, I cannot see the default context menus provided by browsers like ‘open link in new tab’ or ‘open link in new window’ when i right click the link.
I tried like below too.
<a href="#" onclick="myredirect()"> a link </a>
In this case, i can see the link context menu, but it does not execute the “myredirect()” when i do the ‘open link in new tab’ menu.
Is there a way to run a javascript code when i open link in new tab or new window?
Short answer: No, you can’t.
But since you are using JS anyways, you can just use it to change the
hrefattribute in<a, so by the time the user clilcks on the link, it’s already pointing to the right URL.