0

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

thunderblaster
  • 918
  • 11
  • 27
user1006072
  • 953
  • 5
  • 20
  • 29
  • so is it opening the link twice? – Marshall Brekka Nov 04 '11 at 22:03
  • No. The current behavior is you are on www.mysite.com and you click on the link Self Service Option , you are correctly pointed to the Self Service Website. However, if you right click on the link and say open in new tab, then a new tab is opened with www.mysite.com . Same behavior for open in new window. Thanks – user1006072 Nov 04 '11 at 22:05
  • got it, so you running an on click handler for the link, and thats where it gets the correct address from? – Marshall Brekka Nov 04 '11 at 22:13

1 Answers1

0

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

Community
  • 1
  • 1
Marshall Brekka
  • 1,156
  • 2
  • 11
  • 15
  • Thanks Marshall. I will give this a shot but I would also like to know what is wrong in the above method, I originally posted. – user1006072 Nov 04 '11 at 22:20
  • what i think is happening is that the normal onclick event listener is not be triggered when run off a right click event, and I'm assuming your links href attribute looks something like this href="", which i believe will just have it open the current page, so when you click "open in new tab", its grabbing the href value, opening a new tab with that value, and bypassing our onclick event handler all together. This is just my hypothesis... – Marshall Brekka Nov 05 '11 at 00:22
  • Hi Marshall, thanks for your advice. I investigated this by putting some Javascript alerts and this is not what's happening. Anyone, anymore suggestions. – user1006072 Nov 14 '11 at 20:03
  • can you give us some sample html of a link that is exhibiting this behavior? – Marshall Brekka Nov 14 '11 at 20:11
  • Sure. Go to https://my.cigna.com/web/public/guest? and below the login section, notice the "Check Your ID Card" link. See the different behavior when you simply click on it (left click) and when you right click and say open in new tab. – user1006072 Nov 14 '11 at 21:19
  • when i left-click the link, it opens a "lightbox" type popup, not a new window, is this the expected behavior? – Marshall Brekka Nov 14 '11 at 21:25
  • Yes. Its expected. However, when you right click - what happens is not expected. As you see it open up the same window. It should behave exactly as it did when you left clicked. i.e open up the light box. Similarly - for me left clicks opens up a whole new link outside of my website, but right click -open in new tab opens a new tab but opens the same (current) window in my case which is wrong. – user1006072 Nov 14 '11 at 21:42
  • Hi Marshal, Thanks again for your response. I tried doing this and no success. I still see some weird behavior. Right now - say I call a function getSelfServiceOption when the user clicks the link. Here I can check if location.hash is null. But this point is hit only if I left click on the link. When I right click, it doesn't even hit this function. I know this because I have a alert inside the function. So, checking for the Hash id is not helping me. – user1006072 Nov 14 '11 at 23:18
  • you misunderstood the execution flow I described. Don't check for the hash value on a click event, you must check for that value when the page first loads (the onload event "window.onload"). If the value is present, then run the function that normally gets run when the user clicks the link. – Marshall Brekka Nov 15 '11 at 00:04