I'm trying to make a custom context menu. I'll eventually put in all the options.
I tried it in both Firefox and Chrome, and it doesn't even appear. It just shows the default context menu.
Here is my code:
sandbox.html:
<!DOCTYPE html>
<!-- Christian's Sandbox -->
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="Author" content="Christian Arnold" />
<title>Sandbox</title>
<link rel="stylesheet" type="text/css" href="sandbox.css" />
<script type="text/javascript" src="sandbox.js"></script>
</head>
<body>
<div id="contextmenu">
This is the context menu div.<br />
Put all your context menu-y things in here.
</div>
</body>
</html>
sandbox.css:
/* CSS for Christian's Sandbox */
#contextmenu {
display: none;
position: absolute;
left: 0px;
top: 0px;
border: 1px solid #aaa;
border-radius: 2px;
background-color: #ccc;
color: #000;
}
sandbox.js:
/* JavaScript for Christian's Sandbox */
document.onload = function() { // Make sure this all works
// Context menu script
var contextmenudiv = document.getElementById("contextmenu"),
contextmenu = {
hide: function(event) {
// Hide the context menu div
contextmenudiv.style.display = "none";
// Remove the event listener
document.removeEventListener("click", contextmenu.hide, true);
},
};
document.addEventListener("contextmenu", function(event) {
// Prevent the browser from opening the default context menu
event.preventDefault();
// Move the context menu to where the mouse is with respect to the page
contextmenudiv.style.left = (event.pageX + scrollX) + "px";
contextmenudiv.style.top = (event.pageY + scrollY) + "px";
// Display it
contextmenudiv.style.display = "block ";
// When you click somewhere else, hide it
document.addEventListener("click", contextmenu.hide, true);
}, true);
}
window.onloadinstead ofdocument.onloaddocument.bodyinstead ofdocumentwill be a great idea