Right Click Context Menu on Image ONLY

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
bfredett
Beginner
Beginner
Posts: 1
Joined: Wed Jul 07, 2004 8:24 pm

Right Click Context Menu on Image ONLY

Post by bfredett »

Greetings,


Just downloaded the context menu. Great look and easy to setup.

Is there a way to configure the scripts so that the right-click menu only appears when the mouse is over an image(s)? If the user right-clicks off an image ... the regular menu shows up.

Regards,
Brian
User avatar
kevin3442
Milonic God
Milonic God
Posts: 2460
Joined: Sat Sep 07, 2002 12:09 am
Location: Lincoln, NE
Contact:

Post by kevin3442 »

Hi Brian,

I'd say yeah, you can do it. I wouldn't want to modify the contextmenu.js script, 'cause I didn't write it and I don't know if it might be updated down the road. But we can make a function to hook into it. contextmenu.js has a flag called conextDisabled that we can use to toggle the context menu active or not active. We'll toggle it active when a user mouses over an image (conveniently, the mouseover must occur before the right-click on the image). We'll toggle the menu inactive when the user mouses out of the image.

Place the following code at the top of your menu_data.js file

Code: Select all

function setContextDisabled(state)
{
  contextDisabled = state;
  if (!contextDisabled) {
    if(ns4){
      _d.captureEvents(Event.MOUSEDOWN);
      _d.onmousedown=rclick;
    }
    else {
      _d.onmouseup=rclick
      _d.oncontextmenu=new Function("return false")
    }
  }
}

setContextDisabled(true);
You'll want to make sure that menu_data.js is loaded into the page after contextmenu.js, as it is in the example you downloaded.

Now, on an image where you want the right-click menu to be availabe, you insert onmouseover and onmouseout handlers, like so:

Code: Select all

<img src="mypic.gif" onmouseover="setContextDisabled(false)" onmouseout="setContextDisabled(true)">
That should do it.

Hope that helps,

Kevin
Post Reply