在新选项卡或窗口中打开时,Javascript 链接不起作用
我有一些像这样的链接
<a href="#" onclick=aFunction(this)>link</a>
,其中“aFunction”在当前窗口中打开链接。
单击此链接时,可以打开该链接,但如果单击“在新选项卡中打开”或“在新窗口中打开”,则不起作用。
a函数代码是这样的:
aFunction(object)
{
object.href = "www.mypage.com"
}
I have some link like this
<a href="#" onclick=aFunction(this)>link</a>
Where "aFunction" opens a link in the current window.
When this link is clicked, it is ok and opens the link, but if it is clicked as "open in new tab" or "open in new window", it does not work.
aFunction code is something like this:
aFunction(object)
{
object.href = "www.mypage.com"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用“在新 X 中打开”的上下文菜单不会触发单击事件。如果您的代码只是为了设置新窗口的地址,那么您最好
这样做。它不会验证,但会更好地降级。
Using the context menu for "open in new X" does not trigger the click event. If your code is there simply to set the new window's address, then you'd be far better off doing
instead. It won't validate, but it'll degrade better.
为什么要这样做而不是仅仅将 url 放在 href 参数中?如果您愿意,即使正确使用 href 属性,您仍然可以通过添加单击处理程序对单击事件执行其他操作。
请注意,通过不返回 false(或使用 PreventDefault()),我们允许在 javascript 运行后发生正常的单击操作。
Why would you do something like this instead of just putting the url in the href parameter? You can still do other stuff on the click event if you want by adding a click handler even if you use the href attribute properly.
Note that by not returning false (or using preventDefault()), we allow the normal click action to take place once our javascript has run.
使用这个:
这样浏览器的“在新选项卡中打开链接”不会制动。
如果当用户触摸页面上的某些内容时条件发生变化,您需要在用户触摸某些内容时调用
changeUrl
。Use this:
In this way the "Open link in a new tab" of the browser does not brake.
If the condition changes when the user touches something on the page you need to call
changeUrl
whenever he touches something.请在下面找到一个在新窗口中打开网址的函数:
Please find below a function to open url in new window: