Javascript 调整以根据用户的选择在新窗口或新选项卡中打开 URL [英] Javascript tweak to open URL either in a new window or new tab based on user's choice

查看:23
本文介绍了Javascript 调整以根据用户的选择在新窗口或新选项卡中打开 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,如果您只需单击它,我网站上的链接就会在新窗口中打开.但是,如果您右键单击并说在新窗口或新选项卡中打开,它会再次从单击链接的位置打开相同的窗口 (URL).

I am facing an issue where a link on my website opens in a new window if you simply click on it. However, if you right click and say open in new window or new tab, it opens the same window (URL) again from where the link is clicked.

Self Service Option 是一个链接,当单击该链接时,JSP 会调用函数 getSelfServSite().这就是代码在我的情况下的流动方式

Self Service Option is a link and the JSP calls a function getSelfServSite() when the link is clicked. This is how the code flows in my case

function getSelfServSite()
{
   getToTheLink("${myConfigInfo.selfServiceURL}"); 
   // this is because the URL is configurable
}

function getToTheLink(url) 
{
   window.open (url, "currentWindow", "");
}

我做错了什么.无论用户如何单击它,我都希望它转到正确的链接.

What am I doing wrong. I want it to go to the right link no matter how the user click it.

请指教.谢谢

推荐答案

我建议做这样的事情.

I would suggest doing something like this.

设置事件处理程序以在用户右键单击时捕获.当他们这样做时,运行你的函数来获取 selfServSite url,并将 links href 属性设置为新的 Url.

Setup an event handler to capture when a user right clicks. When they do, run you function to get the selfServSite url, and set the links href attribute to be the new Url.

这里有一些关于捕获右键单击事件的信息.

here is some info on capturing the right click event.

如何捕获右键单击事件在 JavaScript 中?

根据我们在评论中的讨论,这里有一个修订的解决方案.

Based on our discussion in the comments, here is a revised solution.

当页面通过右键单击在新窗口中打开时,它附加了#id-card",因此您需要做的是在页面首次加载时检查该值,如果是在那里,运行与用户左键单击链接时相同的 javascript 函数.

When the page is opened in a new window from a right-click, it has "#id-card" appended to it, so what you need to do is check for that value when the page first loads, and if it's there, run the same javascript function that gets run when the user left-clicks on the link.

您可以使用位置对象哈希属性检查此值.http://www.w3schools.com/jsref/obj_location.asp

You can check for this value using the location objects hash property. http://www.w3schools.com/jsref/obj_location.asp

这篇关于Javascript 调整以根据用户的选择在新窗口或新选项卡中打开 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆