Solved

how to  trigger a right click event

Posted on 2013-10-25
14
1,876 Views
Last Modified: 2013-10-25
How can I trigger (generate) a right click event?
I can't find anything  usefeul and I need pure JS (not jquery)
Thank you
0
Comment
Question by:myyis
  • 7
  • 6
14 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39601694
The purpose of which is to do what?
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 39601696
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
 
LVL 58

Expert Comment

by:Gary
ID: 39601706
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:myyis
ID: 39601733
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
 
LVL 58

Expert Comment

by:Gary
ID: 39601774
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
 

Author Comment

by:myyis
ID: 39601782
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
 
LVL 58

Expert Comment

by:Gary
ID: 39601790
Confused
Trigger the right click on the anchor tag to do what?
You have a right click function on the div.
0
 

Author Comment

by:myyis
ID: 39601795
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
 
LVL 58

Expert Comment

by:Gary
ID: 39601800
You cannot do that, I already told you earlier.
0
 

Author Comment

by:myyis
ID: 39601806
Using this possible?

stackoverflow.com/questions/6250447/trigger-right-click
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39601815
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
 

Author Comment

by:myyis
ID: 39601839
Is it possible  to write a custom context menu with "open link in new tab" feature?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39601856
And you do not want to use jquery?
0
 

Author Comment

by:myyis
ID: 39601877
If possible I don't want to use.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question