how to trigger a right click event

How can I trigger (generate) a right click event?
I can't find anything  usefeul and I need pure JS (not jquery)
Thank you
LVL 1
myyisAsked:
Who is Participating?
 
GaryConnect With a Mentor Commented:
That is to trigger the jquery contextmenu plugin - you create your own menu - this is not the normal right click context menu

But you will not have access to the normal right click menu options, well some of them you can emulate with jquery like going back, select all, trigger print.
0
 
GaryCommented:
The purpose of which is to do what?
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
try this code:

if (document.createEvent) {
  var rightClick = document.createEvent('MouseEvents');
  rightClick.initMouseEvent(
    'click', // type
    true,    // canBubble
    true,    // cancelable
    window,  // view - set to the window object
    1,       // detail - # of mouse clicks
    10,       // screenX - the page X coordinate
    10,       // screenY - the page Y coordinate
    10,       // clientX - the window X coordinate
    10,       // clientY - the window Y coordinate
    false,   // ctrlKey
    false,   // altKey
    false,   // shiftKey
    false,   // metaKey
    2,       // button - 1 = left, 2 = right
    null     // relatedTarget
  );
  document.dispatchEvent(rightClick);
} else if (document.createEventObject) { // for IE
  var rightClick = document.createEventObject();
  rightClick.type = 'click';
  rightClick.cancelBubble = true;
  rightClick.detail = 1;
  rightClick.screenX = 10;
  rightClick.screenY = 10;
  rightClick.clientX = 10;
  rightClick.clientY = 10;
  rightClick.ctrlKey = false;
  rightClick.altKey = false;
  rightClick.shiftKey = false;
  rightClick.metaKey = false;
  rightClick.button = 2;
  document.fireEvent('onclick', rightClick);
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
GaryCommented:
That will do nothing.
You cannot emulate a right click only simulate it - in other words you can not make the default right click context menu appear with javascript.
0
 
myyisAuthor Commented:
This is what I want to do


<script type="text/javascript">

function showtab (link)
{
document.getElementById("tablink").href=link;

//trigger right click here for the element "tablink" with url goto.php

}

</script>
</head>
<body  >
      <div  oncontextmenu="showtab('goto.php')" >right click me </div>
      <a id="tablink" href=''></a>                                                      
</body>
</html>
0
 
GaryCommented:
You have prevent the default action with a return false

<script type="text/javascript">

function showtab (link)
{
document.getElementById("tablink").href=link;

alert(link) // added in to show it working

//trigger right click here for the element "tablink" with url goto.php
return false;
}

</script>
0
 
myyisAuthor Commented:
ok but what shall I put for this part? how will I make the trigger?
I want to open the standart box  when right clicked a link.

//trigger right click here for the element "tablink" with url goto.php
0
 
GaryCommented:
Confused
Trigger the right click on the anchor tag to do what?
You have a right click function on the div.
0
 
myyisAuthor Commented:
The right click function (oncontextmenu) on the div does not open the standart right click box of a link (an url). I want it to open that standart box.
0
 
GaryCommented:
You cannot do that, I already told you earlier.
0
 
myyisAuthor Commented:
Using this possible?

stackoverflow.com/questions/6250447/trigger-right-click
0
 
myyisAuthor Commented:
Is it possible  to write a custom context menu with "open link in new tab" feature?
0
 
GaryCommented:
And you do not want to use jquery?
0
 
myyisAuthor Commented:
If possible I don't want to use.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.