0

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);

}
6
  • 3
    Make a fiddle of this jsfiddle.net Commented Feb 11, 2012 at 23:22
  • Does Firebug show any errors? I suspect you have a syntax error involving a comma. Commented Feb 11, 2012 at 23:23
  • Try with window.onload instead of document.onload Commented Feb 11, 2012 at 23:51
  • By the way, adding your click listeners on document.bodyinstead of document will be a great idea Commented Feb 12, 2012 at 0:02
  • 1
    Nevermind, I have the whole thing fixed. I'll post how I fixed it tomorrow. Commented Feb 12, 2012 at 0:58

1 Answer 1

1

I got it fixed.

What I did was:
I put the contextmenudiv = document.getElementById("contextmenu") into the event listener instead of putting the whole thing into document.onload.

Here is my new code:

/* JavaScript for Christian's Sandbox */

// Context menu script
var contextmenudiv,
    contextmenu = {
      hide: function(event) {
        if (event.button == 0) {
          event.preventDefault();
          // Hide the context menu div
          contextmenudiv.style.display = "none";
          // Remove the event listener
          document.removeEventListener("click", contextmenu.hide, true);
        }
      },
    };
document.addEventListener("contextmenu", function(event) {
  contextmenudiv = document.getElementById("contextmenu");
  // 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);

Now, I have effects and a notifications system to go along with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.