Hello,
I have created a context menu in mozilla by using following code:
[CODE=javascript]function nrc(e) {
var contextMenu;
document.oncont extmenu = function (evt) {
var srcElement;
if (evt && evt.target) {
srcElement = evt.target;
if (srcElement.nod eType == 3) {
srcElement = srcElement.pare ntNode;
}
} else if (window.event) {
srcElement = window.event.sr cElement;
}
if (srcElement) {
if (typeof contextMenu == 'undefined') {
contextMenu = createContextMe nu('contextMenu ');
}
if (contextMenu != null) {
var coords = getPageCoords(e vt ? evt : window.event);
contextMenu.sty le.left = coords.x;
contextMenu.sty le.top = coords.y;
contextMenu.src Element = srcElement;
contextMenu.sty le.visibility = 'visible';
if (evt && evt.preventDefa ult) {
evt.preventDefa ult();
}
return false;
}
}
};
}
document.onmous edown = nrc;
window.onmoused own = nrc;
if (document.layer s) {
window.captureE vents(Event.MOU SEDOWN);
}
function getPageCoords (evt) {
var coords = { x: 0, y: 0};
if (typeof window.pageXOff set != 'undefined') {
coords.x = window.pageXOff set + evt.clientX;
coords.y = window.pageYOff set + evt.clientY;
} else if (document.compa tMode && document.compat Mode != 'BackCompat') {
coords.x = document.docume ntElement.scrol lLeft + evt.clientX;
coords.y = document.docume ntElement.scrol lTop + evt.clientY;
} else {
coords.x = document.body.s crollLeft + evt.clientX;
coords.y = document.body.s crollTop + evt.clientY;
}
return coords;
}
function createContextMe nu (menuId) {
var menu;
if (document.creat eElement && (menu = document.create Element('div')) ) {
menu.id = menuId;
menu.style.posi tion = 'absolute';
menu.style.back groundColor = '#E3E0E3';
menu.style.alig n='right';
menu.style.bord er = '1px outset black';
menu.style.visi bility = 'hidden'
var link = document.create Element('a');
menu.link = link;
link.href = '#';
link.style.disp lay = 'block';
link.onclick=so mefunc;
link.appendChil d(document.crea teTextNode('Add item'));
menu.appendChil d(link);
menu.onmouseout =menuMouseout; // to close context menu
menu.onclick = menuClick;
document.body.a ppendChild(menu );
return menu;
} else {
return null;
}
}
function menuClick (evt) {
this.style.visi bility = '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.relatedTarg et) {
if (!contains(this , evt.relatedTarg et)) {
this.style.visi bility = 'hidden';
}
} else if (window.event && event.toElement ) {
if (!this.contains (event.toElemen t)) {
this.style.visi bility = 'hidden';
}
}
}
function contains (container, containee) {
while (containee) {
if (container == containee) {
return true;
}
containee = containee.paren tNode;
}
return false;
}
[/CODE]
This code is working perfectly.
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.
I have created a context menu in mozilla by using following code:
[CODE=javascript]function nrc(e) {
var contextMenu;
document.oncont extmenu = function (evt) {
var srcElement;
if (evt && evt.target) {
srcElement = evt.target;
if (srcElement.nod eType == 3) {
srcElement = srcElement.pare ntNode;
}
} else if (window.event) {
srcElement = window.event.sr cElement;
}
if (srcElement) {
if (typeof contextMenu == 'undefined') {
contextMenu = createContextMe nu('contextMenu ');
}
if (contextMenu != null) {
var coords = getPageCoords(e vt ? evt : window.event);
contextMenu.sty le.left = coords.x;
contextMenu.sty le.top = coords.y;
contextMenu.src Element = srcElement;
contextMenu.sty le.visibility = 'visible';
if (evt && evt.preventDefa ult) {
evt.preventDefa ult();
}
return false;
}
}
};
}
document.onmous edown = nrc;
window.onmoused own = nrc;
if (document.layer s) {
window.captureE vents(Event.MOU SEDOWN);
}
function getPageCoords (evt) {
var coords = { x: 0, y: 0};
if (typeof window.pageXOff set != 'undefined') {
coords.x = window.pageXOff set + evt.clientX;
coords.y = window.pageYOff set + evt.clientY;
} else if (document.compa tMode && document.compat Mode != 'BackCompat') {
coords.x = document.docume ntElement.scrol lLeft + evt.clientX;
coords.y = document.docume ntElement.scrol lTop + evt.clientY;
} else {
coords.x = document.body.s crollLeft + evt.clientX;
coords.y = document.body.s crollTop + evt.clientY;
}
return coords;
}
function createContextMe nu (menuId) {
var menu;
if (document.creat eElement && (menu = document.create Element('div')) ) {
menu.id = menuId;
menu.style.posi tion = 'absolute';
menu.style.back groundColor = '#E3E0E3';
menu.style.alig n='right';
menu.style.bord er = '1px outset black';
menu.style.visi bility = 'hidden'
var link = document.create Element('a');
menu.link = link;
link.href = '#';
link.style.disp lay = 'block';
link.onclick=so mefunc;
link.appendChil d(document.crea teTextNode('Add item'));
menu.appendChil d(link);
menu.onmouseout =menuMouseout; // to close context menu
menu.onclick = menuClick;
document.body.a ppendChild(menu );
return menu;
} else {
return null;
}
}
function menuClick (evt) {
this.style.visi bility = '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.relatedTarg et) {
if (!contains(this , evt.relatedTarg et)) {
this.style.visi bility = 'hidden';
}
} else if (window.event && event.toElement ) {
if (!this.contains (event.toElemen t)) {
this.style.visi bility = 'hidden';
}
}
}
function contains (container, containee) {
while (containee) {
if (container == containee) {
return true;
}
containee = containee.paren tNode;
}
return false;
}
[/CODE]
This code is working perfectly.
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.
Comment