Cyber-Drugs
asked on
Dynamic Context Menu
Hi guys,
I'm looking to create a Dynamic Context menu, but Dynamic I mean I can have loads of arrays, eg:
myArrayA[0].Title='File';
myArrayA[0].Func='runFunct ionA();';
myArrayA[1].Title='Save';
myArrayA[1].Func='runFunct ionB();';
myArrayA[2].Title='Load';
myArrayA[2].Func='runFunct ionC();';
myArrayA[3].Title='Exit';
myArrayA[3].Func='return false;';
myArrayB[0].Title='Copy';
myArrayB[0].Func='window.c lipboardDa ta.setData ("Text", trgObj.value);';
myArrayB[1].Title='Paste';
myArrayB[1].Func='trgObj.v alue = window.clipboardData.getDa ta("Text") ';
And I can set different elements of the screen with different menus, eg:
document.getElementById('t agA').onco ntextmenu = displayMenu('myArrayA');
document.getElementById('t agB').onco ntextmenu = displayMenu('myArrayB');
Any ideas on how I can get this to work guys?
Cheers!
I'm looking to create a Dynamic Context menu, but Dynamic I mean I can have loads of arrays, eg:
myArrayA[0].Title='File';
myArrayA[0].Func='runFunct
myArrayA[1].Title='Save';
myArrayA[1].Func='runFunct
myArrayA[2].Title='Load';
myArrayA[2].Func='runFunct
myArrayA[3].Title='Exit';
myArrayA[3].Func='return false;';
myArrayB[0].Title='Copy';
myArrayB[0].Func='window.c
myArrayB[1].Title='Paste';
myArrayB[1].Func='trgObj.v
And I can set different elements of the screen with different menus, eg:
document.getElementById('t
document.getElementById('t
Any ideas on how I can get this to work guys?
Cheers!
whenever i've done a context menu, i've had a div in the code that i show when the user clicks. you could write your div out dynamically use JS if you want, with those items.
ASKER
Hi brunobear,
I have some code which does that already, but unfortunately, I don't see how I could make it work with more than one group of div's. I'm going to post the code on here, and if you can see how to alter it so I can change it to be dynamic to do something like this:
document.getElementById('t agA').onco ntextmenu = displayMenu('myDivA');
document.getElementById('t agB').onco ntextmenu = displayMenu('myDivB');
Cheers!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ContextMenu </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!--
.skin0{
position:absolute;
width:140px;
border:1pt solid #808080;
background-color:#cccccc;
font-family:Verdana;
line-height:20px;
cursor:default;
font-size:14px;
z-index:100;
visibility:hidden;
}
.menuitems{
padding-left:10px;
padding-right:10px;
margin:2px;
font-family:Arial;
font-size:8pt;
border:1pt solid #cccccc;
}
.hritems{
padding: -10px;
margin : 0px;
}
-->
</style>
</head>
<body>
<div id="ie5menu" class="skin0" onMouseover="highlightie5( event)" onMouseout="lowlightie5(ev ent)" onClick="jumptoie5(event)" display:none>
<div class="menuitems" url="http://dynamicdrive.com" onclick="CopyToClipBoard() ;">Copy</d iv>
<div class="menuitems" url="http://dynamicdrive.com" onclick="PasteFromClipBoar d();">Past e</div>
</div>
<script language="JavaScript1.2">
function GetWindowSize()
{
this.myWidth = 0;
this.myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
this.myWidth = window.innerWidth;
this.myHeight = window.innerHeight;
}
else if( document.documentElement &&
(document.documentElement. clientWidt h || document.documentElement.c lientHeigh t ) ) {
//IE 6+ in 'standards compliant mode'
this.myWidth = document.documentElement.c lientWidth ;
this.myHeight = document.documentElement.c lientHeigh t;
}
else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
this.myWidth = document.body.clientWidth;
this.myHeight = document.body.clientHeight ;
}
}
function test() {
alert('this is a test');
}
//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0
var ie5=document.all&&document .getElemen tById
var ns6=document.getElementByI d&&!docume nt.all
if (ie5||ns6)
var menuobj=document.getElemen tById("ie5 menu")
function showmenuie5(e){
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? document.body.clientWidth- event.clie ntX : window.innerWidth-e.client X
var bottomedge=ie5? document.body.clientHeight -event.cli entY : window.innerHeight-e.clien tY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.offsetW idth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? document.body.scrollLeft+e vent.clien tX-menuobj .offsetWid th : window.pageXOffset+e.clien tX-menuobj .offsetWid th
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? document.body.scrollLeft+e vent.clien tX : window.pageXOffset+e.clien tX
//same concept with the vertical position
if (bottomedge<menuobj.offset Height)
menuobj.style.top=ie5? document.body.scrollTop+ev ent.client Y : window.pageYOffset+e.clien tY;
else
menuobj.style.top=ie5? document.body.scrollTop+ev ent.client Y : window.pageYOffset+e.clien tY;
menuobj.style.visibility=" visible"
var posY = parseInt (menuobj.style.top) + menuobj.offsetHeight;
var wsize = new GetWindowSize();
window.status = posY + ' ' + wsize.myHeight + ' ' + menuobj.offsetHeight;
if (posY > wsize.myHeight) {
posY = parseInt(wsize.myHeight) - menuobj.offsetHeight - 20;
window.status = posY ;
menuobj.style.top = posY;
}
return false
}
var trgObj = null;
var srcObj = null;
function getTarget(evt) {
if (!evt) { evt = window.event; }
if (document.all) { return (evt.srcElement); }
return (evt.target);
}
function CopyToClipBoard() {
clipboard = trgObj.value;
window.clipboardData.setDa ta("Text", clipboard);
}
function PasteFromClipBoard() {
clipboard = window.clipboardData.getDa ta("Text") ;
if (!clipboard || !trgObj.value) { return; }
trgObj.value = clipboard;
}
function hidemenuie5(e){
menuobj.style.visibility=" hidden"
trgObj = getTarget (e);
if (trgObj.tagName != 'INPUT' && trgObj.type != 'text') { trgObj = null; return; }
if (trgObj) { window.status = 'Target is ' + trgObj.tagName; }
}
function highlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men uitems"||n s6&&firing obj.parent Node.class Name=="men uitems"){
if (ns6&&firingobj.parentNode .className =="menuite ms") firingobj=firingobj.parent Node //up one node
firingobj.style.background Color="#CF D6E8"
firingobj.style.border="1p t solid #737B92"
firingobj.style.color="bla ck"
if (display_url==1)
window.status=event.srcEle ment.url
}
}
function lowlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men uitems"||n s6&&firing obj.parent Node.class Name=="men uitems"){
if (ns6&&firingobj.parentNode .className =="menuite ms") firingobj=firingobj.parent Node //up one node
firingobj.style.background Color="#cc cccc"
firingobj.style.border="1p t solid #cccccc"
firingobj.style.color="bla ck"
window.status=''
}
}
function jumptoie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men uitems"||n s6&&firing obj.parent Node.class Name=="men uitems"){
if (ns6&&firingobj.parentNode .className =="menuite ms") firingobj=firingobj.parent Node
if (firingobj.getAttribute("t arget"))
window.open(firingobj.getA ttribute(" url"),firi ngobj.getA ttribute(" target"))
}
}
if (ie5||ns6){
menuobj.style.display=''
document.oncontextmenu=sho wmenuie5
document.onclick=hidemenui e5
}
</script>
<input type="text" name="txt1" value="Text Value1">
<input type="text" name="txt2">
</body>
</html>
I have some code which does that already, but unfortunately, I don't see how I could make it work with more than one group of div's. I'm going to post the code on here, and if you can see how to alter it so I can change it to be dynamic to do something like this:
document.getElementById('t
document.getElementById('t
Cheers!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ContextMenu </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
<!--
.skin0{
position:absolute;
width:140px;
border:1pt solid #808080;
background-color:#cccccc;
font-family:Verdana;
line-height:20px;
cursor:default;
font-size:14px;
z-index:100;
visibility:hidden;
}
.menuitems{
padding-left:10px;
padding-right:10px;
margin:2px;
font-family:Arial;
font-size:8pt;
border:1pt solid #cccccc;
}
.hritems{
padding: -10px;
margin : 0px;
}
-->
</style>
</head>
<body>
<div id="ie5menu" class="skin0" onMouseover="highlightie5(
<div class="menuitems" url="http://dynamicdrive.com" onclick="CopyToClipBoard()
<div class="menuitems" url="http://dynamicdrive.com" onclick="PasteFromClipBoar
</div>
<script language="JavaScript1.2">
function GetWindowSize()
{
this.myWidth = 0;
this.myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
this.myWidth = window.innerWidth;
this.myHeight = window.innerHeight;
}
else if( document.documentElement &&
(document.documentElement.
//IE 6+ in 'standards compliant mode'
this.myWidth = document.documentElement.c
this.myHeight = document.documentElement.c
}
else if( document.body && ( document.body.clientWidth || document.body.clientHeight
//IE 4 compatible
this.myWidth = document.body.clientWidth;
this.myHeight = document.body.clientHeight
}
}
function test() {
alert('this is a test');
}
//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0
var ie5=document.all&&document
var ns6=document.getElementByI
if (ie5||ns6)
var menuobj=document.getElemen
function showmenuie5(e){
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? document.body.clientWidth-
var bottomedge=ie5? document.body.clientHeight
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.offsetW
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? document.body.scrollLeft+e
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? document.body.scrollLeft+e
//same concept with the vertical position
if (bottomedge<menuobj.offset
menuobj.style.top=ie5? document.body.scrollTop+ev
else
menuobj.style.top=ie5? document.body.scrollTop+ev
menuobj.style.visibility="
var posY = parseInt (menuobj.style.top) + menuobj.offsetHeight;
var wsize = new GetWindowSize();
window.status = posY + ' ' + wsize.myHeight + ' ' + menuobj.offsetHeight;
if (posY > wsize.myHeight) {
posY = parseInt(wsize.myHeight) - menuobj.offsetHeight - 20;
window.status = posY ;
menuobj.style.top = posY;
}
return false
}
var trgObj = null;
var srcObj = null;
function getTarget(evt) {
if (!evt) { evt = window.event; }
if (document.all) { return (evt.srcElement); }
return (evt.target);
}
function CopyToClipBoard() {
clipboard = trgObj.value;
window.clipboardData.setDa
}
function PasteFromClipBoard() {
clipboard = window.clipboardData.getDa
if (!clipboard || !trgObj.value) { return; }
trgObj.value = clipboard;
}
function hidemenuie5(e){
menuobj.style.visibility="
trgObj = getTarget (e);
if (trgObj.tagName != 'INPUT' && trgObj.type != 'text') { trgObj = null; return; }
if (trgObj) { window.status = 'Target is ' + trgObj.tagName; }
}
function highlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men
if (ns6&&firingobj.parentNode
firingobj.style.background
firingobj.style.border="1p
firingobj.style.color="bla
if (display_url==1)
window.status=event.srcEle
}
}
function lowlightie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men
if (ns6&&firingobj.parentNode
firingobj.style.background
firingobj.style.border="1p
firingobj.style.color="bla
window.status=''
}
}
function jumptoie5(e){
var firingobj=ie5? event.srcElement : e.target
if (firingobj.className=="men
if (ns6&&firingobj.parentNode
if (firingobj.getAttribute("t
window.open(firingobj.getA
}
}
if (ie5||ns6){
menuobj.style.display=''
document.oncontextmenu=sho
document.onclick=hidemenui
}
</script>
<input type="text" name="txt1" value="Text Value1">
<input type="text" name="txt2">
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am more than happy to re-do all the positioning, but that seems to be exactly what I am after, cheers!
glad i could help, thanks for the A. :-)