myyis
asked on
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
I can't find anything usefeul and I need pure JS (not jquery)
Thank you
The purpose of which is to do what?
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);
}
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.
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.
ASKER
This is what I want to do
<script type="text/javascript">
function showtab (link)
{
document.getElementById("t ablink").h ref=link;
//trigger right click here for the element "tablink" with url goto.php
}
</script>
</head>
<body >
<div oncontextmenu="showtab('go to.php')" >right click me </div>
<a id="tablink" href=''></a>
</body>
</html>
<script type="text/javascript">
function showtab (link)
{
document.getElementById("t
//trigger right click here for the element "tablink" with url goto.php
}
</script>
</head>
<body >
<div oncontextmenu="showtab('go
<a id="tablink" href=''></a>
</body>
</html>
You have prevent the default action with a return false
<script type="text/javascript">
function showtab (link)
{
document.getElementById("t ablink").h ref=link;
alert(link) // added in to show it working
//trigger right click here for the element "tablink" with url goto.php
return false;
}
</script>
<script type="text/javascript">
function showtab (link)
{
document.getElementById("t
alert(link) // added in to show it working
//trigger right click here for the element "tablink" with url goto.php
return false;
}
</script>
ASKER
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
I want to open the standart box when right clicked a link.
//trigger right click here for the element "tablink" with url goto.php
Confused
Trigger the right click on the anchor tag to do what?
You have a right click function on the div.
Trigger the right click on the anchor tag to do what?
You have a right click function on the div.
ASKER
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.
You cannot do that, I already told you earlier.
ASKER
Using this possible?
stackoverflow.com/question s/6250447/ trigger-ri ght-click
stackoverflow.com/question
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Is it possible to write a custom context menu with "open link in new tab" feature?
And you do not want to use jquery?
ASKER
If possible I don't want to use.