Javascript tweak to open URL either in a new windo

2019-08-17 00:18发布

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 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.

Please advise. Thanks

1条回答
再贱就再见
2楼-- · 2019-08-17 00:25

I would suggest doing something like this.

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.

How can I capture the right-click event in JavaScript?

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

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.

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

查看更多
登录 后发表回答