I have created a context menu in mozilla by using following code:
Expand|Select|Wrap|Line Numbers
- function nrc(e) {
- var contextMenu;
- document.oncontextmenu = function (evt) {
- var srcElement;
- if (evt && evt.target) {
- srcElement = evt.target;
- if (srcElement.nodeType == 3) {
- srcElement = srcElement.parentNode;
- }
- } else if (window.event) {
- srcElement = window.event.srcElement;
- }
- if (srcElement) {
- if (typeof contextMenu == 'undefined') {
- contextMenu = createContextMenu('contextMenu');
- }
- if (contextMenu != null) {
- var coords = getPageCoords(evt ? evt : window.event);
- contextMenu.style.left = coords.x;
- contextMenu.style.top = coords.y;
- contextMenu.srcElement = srcElement;
- contextMenu.style.visibility = 'visible';
- if (evt && evt.preventDefault) {
- evt.preventDefault();
- }
- return false;
- }
- }
- };
- }
- document.onmousedown = nrc;
- window.onmousedown = nrc;
- if (document.layers) {
- window.captureEvents(Event.MOUSEDOWN);
- }
- function getPageCoords (evt) {
- var coords = { x: 0, y: 0};
- if (typeof window.pageXOffset != 'undefined') {
- coords.x = window.pageXOffset + evt.clientX;
- coords.y = window.pageYOffset + evt.clientY;
- } else if (document.compatMode && document.compatMode != 'BackCompat') {
- coords.x = document.documentElement.scrollLeft + evt.clientX;
- coords.y = document.documentElement.scrollTop + evt.clientY;
- } else {
- coords.x = document.body.scrollLeft + evt.clientX;
- coords.y = document.body.scrollTop + evt.clientY;
- }
- return coords;
- }
- function createContextMenu (menuId) {
- var menu;
- if (document.createElement && (menu = document.createElement('div'))) {
- menu.id = menuId;
- menu.style.position = 'absolute';
- menu.style.backgroundColor = '#E3E0E3';
- menu.style.align='right';
- menu.style.border = '1px outset black';
- menu.style.visibility = 'hidden'
- var link = document.createElement('a');
- menu.link = link;
- link.href = '#';
- link.style.display = 'block';
- link.onclick=somefunc;
- link.appendChild(document.createTextNode('Add item'));
- menu.appendChild(link);
- menu.onmouseout =menuMouseout; // to close context menu
- menu.onclick = menuClick;
- document.body.appendChild(menu);
- return menu;
- } else {
- return null;
- }
- }
- function menuClick (evt) {
- this.style.visibility = 'hidden';
- return false;
- }
- function menuMouseout (evt) { // on mouse out of the contextmenu this func will be called and context menu will be closed
- if (evt && evt.relatedTarget) {
- if (!contains(this, evt.relatedTarget)) {
- this.style.visibility = 'hidden';
- }
- } else if (window.event && event.toElement) {
- if (!this.contains(event.toElement)) {
- this.style.visibility = 'hidden';
- }
- }
- }
- function contains (container, containee) {
- while (containee) {
- if (container == containee) {
- return true;
- }
- containee = containee.parentNode;
- }
- return false;
- }
Now my context menu is closed when i am moving my mouse out from that window. But what i want is i want to close my contextmenu by clicking any where in window not on mouse out of that context menu.
Do anybody has any solution kindly help me.
Do anybody has any other way to create a context menu. If it is help me.