[SalesForce] How to get Anchor tag (which works with JS function call) to work using rightClick > open in new tab

I'm using an anchor tag

<a href="#" onClick="javascriptMethod();">asdasdasd</a>

This javascript function dynamically decides on the page to open. Works all right on click. But when I right click and Open in new tab, it just shows me a page with "URL No Longer Exists"

Any suggestions?

Best Answer

This is nothing to do with Salesforce / Visualforce.. its purely javascript related..

see these discussion on how to handle non left mouse clicks in your onclick event handler

https://stackoverflow.com/questions/17167938/js-onclick-not-executing-when-we-opening-right-click-open-in-new-tab

https://stackoverflow.com/questions/17157455/left-click-to-activate-onclick-and-right-click-to-activate-href

so what these discussions suggest you is to check if its left click then do your action.. otherwise prevent the default action (don't open the context menu)