10

This is my code

<a href="javascript:void(0)" onclick="myFun('www.google.com');" onmousedown="myFun('www.google.com');">

whenever I am clicking on this link my function gets called using onclick event. I added onmousedown event for right-click capturing. But the problem is, this function gets called before selecting "Open in new tab" or "Open in new window" options. When the user uses right-click and before selecting the right-click options, the function gets called. I don't want this behavior. I want to call this function when the user actually selects "Open link in new tab" or "Open link in new window" options.

I don't want to show links in the status bar and also don't want to allow the user to copy the link address. That is why I used onclick and onmousedown events.

Please help me out. Thanks

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Nitesh
  • 1,477
  • 5
  • 23
  • 34
  • these are browser events, not webpage events – Cruiser Mar 07 '17 at 14:32
  • There is nothing that tells you the user selects those. – epascarello Mar 07 '17 at 14:40
  • I can't use only oncontextmenu event. If I do so it is giving me one exception - JSPG0227E: Exception caught while translating /abc.jsp: /abc.jsp(160,10) --> JSPG0123E: Unable to locate tag attribute info for tag attribute oncontextmenu – Nitesh Mar 07 '17 at 15:43

2 Answers2

0

The problem you have is that both, right and left click is handled with the onclick function. Another possibility would be to use oncontextmenu (as mentioned here).

But I think you need to handle right click and left click differently, so onclick is the right event, just check there if it was the primary (0) or secondary (2) mouseButton (see here). So you solution could be something like this:

myFun(link) {
    var buttonPressed = instanceOfMouseEvent.button;
    if (buttonPressed == 0) {
         //Handle normal click
    } else if (buttonPressed == 2) {
         //Handle right click 
    }
}
Community
  • 1
  • 1
EagleT
  • 84
  • 1
  • 10
  • I can find out the right click event. But I want to find out the event when user actually selects the "Open in New Tab" or "Open in New Window" options. My function gets triggered before selecting those options. Because I am my code is able to find the right click event. And I don't want it. My function should trigger after selecting the options – Nitesh Mar 07 '17 at 15:27
  • 1
    @NiteshAarne you may want to look into [building your own context menu](https://www.sitepoint.com/building-custom-right-click-context-menu-javascript/). That way you can exercise more control over what happens. – freginold Mar 07 '17 at 15:36
  • In my example I want to send the link which to be opened. What I am doing is, I want to pass that link to one JS function and want to create one form using JS. and then submit the form using form.submit(); My URL contains some parameters which I don't want to show in address bar. I am creating form and submitting it for POST request. – Nitesh Mar 07 '17 at 15:37
  • I can't use only oncontextmenu event. If I do so it is giving me one exception - JSPG0227E: Exception caught while translating /abc.jsp: /abc.jsp(160,10) --> JSPG0123E: Unable to locate tag attribute info for tag attribute oncontextmenu. – Nitesh Mar 07 '17 at 15:42
  • @NiteshAarne as freginold told, it's the best way to build you own context menu. Since you can then use you own functions and don't have trouble with controlling users select. – EagleT Mar 08 '17 at 07:08
  • @eagleT : I think this is not the correct way of implementing it. Because user might wants to perform other operations other than "Open link in new tab" or "Open link in new window". I can't add all the options which are normally given by browsers. User should not get confused after seeing our customized options. In short, I can't customize it. – Nitesh Mar 08 '17 at 10:18
-1

It sounds like you're looking for something like this, that will change the link once the context menu is launched:

function myFun2(url) {
   linkEl.href = url;
}

Then assign the a element an ID (i.e. "linkEl"), remove the onmousedown handler, and assign an oncontextmenu handler, like:

oncontextmenu="myFun2('www.google.com');"

Then when the context menu is launched, the link will change, so if "Open in New Tab" or "Open in New Window" is selected, it will go to Google (in this example) instead of whatever the link was set to previously.

freginold
  • 3,946
  • 3
  • 13
  • 28
  • :- In my example I want to send the link which to be opened. What I am doing is, I want to pass that link to one JS function and want to create one form using JS. and then submit the form using form.submit(); My URL contains some parameters which I don't want to show in address bar. I am creating form and submitting it for POST request. – Nitesh Mar 07 '17 at 15:36