1

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?

1 Answer 1

1

Short answer: No, you can't.

But since you are using JS anyways, you can just use it to change the href attribute in <a, so by the time the user clilcks on the link, it's already pointing to the right URL.

Sign up to request clarification or add additional context in comments.

3 Comments

thnx. I want to make a dynamic url based on user input.
Right, i'll follow that way. thnx.
Alternative - make the link a span with a pointer cursor which will not give the possibility of opening the link in a new browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.