I'm using jqPlot to create a graph inside a div tag. jqPlot used the canvas element to render the graph. I'm trying to add some interactivity to the graph, i.e. zooming, where the user can zoom in and out using the using the left and right mouse-click, respectively. I want to disable the context menu when the user right-clicks on the graph. I've tried several approaches, including...
Adding the the oncontextmenu attribute to the parent div containing the graph canvas:
<div id="myGraph" style="width: 908px; height: 600px; " class="jqplot-target" align="center" oncontextmenu="return false"></div>
Dynamically applying the oncontextmenu param to all the children after jqPlot renders the graph:
//Code to render the graph
//...
$("#myGraph").children().attr("oncontextmenu", "return false");
Using jQuery's approach instead of regular JS:
$("#myGraph").children().bind("contextmenu",function(e){
return false;
});
And using a custom plug-in from this great site and applying it's disable function as follows:
$("#plot_testEventHistogram").children().disableContextMenu();
But none of these approaches seem to work, I still get a context menu when I right-click on the graph. It's not a showstopper, but is very annoying and would be nice if it can be effectively disabled
You can use this: $('img'). bind('contextmenu', function(e){ return false; });
To override the default browser right-click menu, the first step is to prevent the default right-click behavior. This can be done by listening to the contextmenu event and using the preventDefault() method on the event.
getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported, or the canvas has already been set to a different context mode.
canvas.oncontextmenu = function() {
return false;
}
http://jsfiddle.net/9ZWv2/
Works in chrome
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With