Right-click on the d3.js element: how to prevent the browser context menu from being displayed

I have some d3.js element, for example:

// draw rectangle svg.selectAll(".rect").append("rect") .attr("y", 10) .attr("x", 10) .attr("height", 5) .attr("width", 5) .on("contextmenu", function (d, i) { // react on right-clicking }); 

and it works great, but also opens the browser context menu. How can I prevent this?

+7
javascript
source share
2 answers

Add d3.event.preventDefault(); to your function.

  // draw rectangle svg.selectAll(".rect").append("rect") .attr("y", 10) .attr("x", 10) .attr("height", 5) .attr("width", 5) .on("contextmenu", function (d, i) { d3.event.preventDefault(); // react on right-clicking }); 
+17
source share

I understood and gave below code for you, check it and ask if you need something else ...

 document.onmousedown=disableclick; status="Right Click Disabled"; Function disableclick(event) { if(event.button==2) { alert(status); return false; } } 
0
source share

All Articles