运行 javascript 的链接可打开另一个页面。如果用户使用中键单击或右键单击菜单,如何使其在新选项卡中执行此操作
我的左侧导航中有此链接: 仪表板
该 javascript 根据传递的参数打开一个链接。
一切正常,但我希望能够使用浏览器功能在选项卡中打开链接(当用户使用中键单击或从右键单击菜单中选择“在新标签中打开链接”时)。不过,这对于使用 javascript 代码处理的链接不起作用。
有很多原因导致这不是浏览器的默认行为(例如,javascript 函数可能只执行一些验证并保留在页面中...浏览器无法知道 js 可能会做什么,或者是否会产生一个新窗口/对话框从该操作中,因此由于中键单击而打开新标签是没有意义的......)。但希望有一个解决默认行为的方法。
知道如何做到这一点吗?
干杯, 斯特夫。
I have this link in my left navigation:
dashboard
That javascript opens a link based on the passed parameters.
All works fine, but I would like to be able to use the browser capabilities of opening the links in a tab (when user is using middle click or selects 'Open link in new tag' from right click menu). Though, this is not working for links handled with javascript code.
There are many reasons why this is not the default behaviour of the browser (e.g. javascript function might only do some validation and stay in the page ... browser can't know what the js might do or if a new window/dialog will result from that action so would make no sense to open new tag as a result of a middle click ...). But hopefully there is a workaround for the default behaviour.
Any idea how this could be done?
Cheers,
Stef.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Javascript 链接在调用它们的页面上下文中执行。如果您在新选项卡/窗口中“打开”链接,则 JavaScript 代码将在新窗口(即空窗口)中执行,并且很可能会失败。
浏览器可以尝试通过克隆包含链接的页面并在克隆页面的上下文中执行 JavaScript 代码来添加您要求的功能。但这很可能会破坏一些关键站点(例如,想象一下您的在线银行站点使用 JavaScript,因此当您在新选项卡/窗口中打开链接时,克隆原始窗口可能会导致重复交易)。
Javascript links execute in context of the page where they are called. If you "open" the link in a new tab/window, the javascript code will be executed in the new window, i.e., empty, and will most probably fail.
A browser could try to add the feature you are asking for by cloning the page which contains the link, and executing the javascript code in the context of the cloned page. But this would most likely break some critical sites (imagine for example that your online banking site works with javascript, so when you open a link in a new tab/window, cloning the original window might lead to a duplicate transaction).