Onleftclick & onrightClick Javascript functions
-
22-08-2019 - |
Question
In my server side code I am dynamically building a table and and right now I am adding the following code to handle the row click.
tr.Attributes.Add("onclick", "window.open('" + root + document.IPT_Name + "/" + document.IPT_Sub_Name + "/" + document.File_Name + "', 'mywindow', 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,copyhistory=no, resizable=yes')");
Is there any way to make this work only a left click and then add a different attribute to the right click to go to a different location?
Solution
You can detect it, but there are some browser specific issues you have to watch. Here is some code from http://www.quirksmode.org/js/events_properties.html
function doSomething(e) {
var rightclick;
if (!e) var e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e.button == 2);
alert('Rightclick: ' + rightclick); // true or false
}
There's also a plug-in for jQuery that lets you detect right mouse clicks: http://abeautifulsite.net/notebook/68
OTHER TIPS
Here's some code taken out of jQuery 1.3.2 with comments that explain about event.which
and event.button
.
// Add which for click: 1 == left; 2 == middle; 3 == right
// Note: button is not normalized, so don't use it
if ( !event.which && event.button )
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow