[英]'Open in New Tab or Page' not appearing when Hyperlink/Action Link/Link Buttons are right-clicked
I'm trying to call a JavaScript function from an ASP.NET MVC view (.html) using Action Link/Hyperlink()/Link Buttons. 我正在尝试使用Action Link / Hyperlink()/ Link Buttons从ASP.NET MVC视图(.html)调用JavaScript函数。 Left click works good, but right clicking the link and choosing 'Open a New Tab or Page' is not working.
左键单击效果很好,但是右键单击链接并选择“打开新标签页或页面”无效。
My attempt so far has been: 到目前为止,我的尝试是:
<a href="#" onclick=aFunction()>link</a>
aFunction(object)
{
alert('Inside Javascript Function')
}
This answer might help. 这个答案可能会有所帮助。
Try giving your hyperlink an id
attribute (say, id="foo"
), and create a click handler that performs a specified action whenever a#foo
is right-clicked: 尝试给您的超链接提供
id
属性(例如id="foo"
),并创建一个单击处理程序,该处理程序将在右键单击a#foo
时执行指定的操作:
document.getElementById("foo").onmousedown = function(event) {
if (event.which == 3) {
alert('Inside Javascript Function');
}
}
EDIT : 编辑 :
Per your comment: 根据您的评论:
I don't want to execute the function as soon as the user right click.
我不想在用户右键单击后立即执行该功能。 I want execute the function after the user right click the link and click open new tab option.
我想在用户右键单击链接并单击“打开新选项卡”选项后执行该功能。
I don't believe this is possible (unless there is some arcane Chrome JavaScript API that detects such an event). 我认为这是不可能的(除非有一些不可思议的Chrome JavaScript API可以检测到此类事件)。 It wouldn't make sense for JavaScript to know how Chrome works.
对于JavaScript而言,了解Chrome的工作原理毫无意义。 Further, JavaScript is sandboxed in such a way that it doesn't have access to the user's local file system - by extension, it should not know what the Google Chrome process itself is doing.
此外,对JavaScript进行沙箱处理的方式是,它无法访问用户的本地文件系统-扩展来说,它不应该知道Google Chrome进程本身在做什么。
Use the approach mentioned in this answer: How to run function on new tab from main tab? 使用此答案中提到的方法: 如何在主选项卡的新选项卡上运行功能? (Google Chrome)
(谷歌浏览器)
Basically you would store the function in local storage and on page load event of the page this links open, you would check if there is a function stored in the local storage, and if so, then execute it. 基本上,您会将功能存储在本地存储中,并且在此链接打开的页面的页面加载事件中,您将检查本地存储中是否存储有功能,如果有,则执行该功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.