右键点击执行javascript的html链接 [英] right click on html link which execute javascript
问题描述
当我点击一个链接时,我想执行一个javascript函数来获取动态url。
所以我喜欢以下。
< a href =javascript:myredirect()>链接< / a>
使用此代码,我看不到浏览器提供的默认上下文菜单,如新标签中的打开链接'或'在新窗口打开链接'时,我右键单击链接。
我也在下面试过。
< a href =#onclick =myredirect()> ;链接< / a>
在这种情况下,我可以看到链接上下文菜单,但它不执行myredirect )当我做'新标签'菜单中的'打开链接。'
当我打开新标签页或新窗口中的链接时,是否有办法运行JavaScript代码?
简短回答:不,您不可以。
但是由于您使用的是JS,您可以使用它来更改< a
中的 href
属性,所以当用户在链接上停留时,它已经指向正确的URL。
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 href
attribute in <a
, so by the time the user clilcks on the link, it's already pointing to the right URL.
这篇关于右键点击执行javascript的html链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!