[Javascript] No right click on images

Matt Barton javascript at mattbarton.org
Wed Sep 14 05:52:39 CDT 2005


Apologies that I don't have much time for experimentation, but I'll just 
quote you a quick bit from my O'Reilly DHTML reference ...

"To block the display of the context menu (and perhaps display a custom 
one of your own design via DHTML), set event.returnValue to false in the 
oncontextmenu event handler."

It should be noted that oncontextmenu is IE5(Win) only, but you could 
try to get the same results by using onmousedown/onmouseup and 
interrogating the event.button property?

Matt

Alan Easton wrote:
> Thanks Matt,
> 
> This does work, oncontextmenu="return false;"
> 
> however, the images involved are from a content management system, and 
> the client already has hundreds all over the site.
> 
> I was hoping there could be a universal function I could paste into the 
> HEAD of the page, and it would effect all images, instead of having to 
> re-work all the existing images.
> 
> I have been playing with this function, which works, but it has the 
> alert box popping up, and if I comment it out, the function does not work.
> 
> Any ideas........
> 
> //------------------code-----------------------
> 
> <script type="text/javascript">
> 
> var clickmessage="Images Copyright NC3Rs 2005"
> 
> function disableclick(e) {
> if (document.all) {
> if (event.button==2||event.button==3) {
> if (event.srcElement.tagName=="IMG"){
> alert(clickmessage);
> return false;
> }
> }
> }
> else if (document.layers) {
> if (e.which == 3) {
> alert(clickmessage);
> return false;
> }
> }
> else if (document.getElementById){
> if (e.which==3&&e.target.tagName=="IMG"){
> alert(clickmessage);
> return false;
> }
> }
> }
> 
> function associateimages(){
> for(i=0;i<document.images.length;i++)
> document.images[i].onmousedown=disableclick;
> }
> 
> if (document.all)
> document.onmousedown=disableclick
> else if (document.getElementById)
> document.onmouseup=disableclick
> else if (document.layers)
> associateimages()
> 
> 
>  </script>
> 
> //-----------------code-----------------------
> 
> Again, all help appreciated..........
> 
> Alan...
> 
>     ------------------------------------------------------------------------
>     From:  /Matt Barton <javascript at mattbarton.org>/
>     Reply-To:  /"\[JavaScript List\]" <javascript at LaTech.edu>/
>     To:  /"[JavaScript List]" <javascript at LaTech.edu>/
>     Subject:  /Re: [Javascript] No right click on images/
>     Date:  /Wed, 14 Sep 2005 11:21:49 +0100/
>      >Hi Alan,
>      >
>      >Pretend that at this point I'm telling you all the things that loads
>      >of other people are likely to say about it being a bad idea, bad
>      >practice, pointless if the client doesn't have javascript enabled
>      >etc, etc, and so on ad. nauseum.
>      >
>      >What you want is something like:
>      >
>      > <img oncontextmenu="return false;" src= ....
>      >
>      >  ... or ...
>      >
>      > <img oncontextmenu="returnValue = false;" src= ....
>      >
>      >Try those, and have an experiment, I'm sure it's something like
>      >that.
>      >
>      >Matt
>      >
>      >Alan Easton wrote:
>      >>Hello All,
>      >>  I realise this type of function is quite pointless, but a client
>      >>would like it.
>      >>  I need to disable right click on images, but I DO NOT want any
>      >>alert box popping up, I merely do not want the right click menu to
>      >>appear.
>      >>  I also need the right click to work on any other part of the
>      >>screen, so they can still right click and refresh, view source,
>      >>etc....
>      >>  Hope I have made myself clear, let me know if you need anything
>      >>else from me.
>      >>  As usual, any help would be really appreciated.
>      >>  Alan...
>      >>
>      >>--
>      >>This email has been verified as Virus free
>      >>Virus Protection and more available at http://www.plus.net
>      >>
>      >>
>      >>
>      >>------------------------------------------------------------------------
>      >>
>      >>_______________________________________________
>      >>Javascript mailing list
>      >>Javascript at LaTech.edu
>      >>https://lists.LaTech.edu/mailman/listinfo/javascript
>      >>
>      >>--
>      >>This email has been verified as Virus free
>      >>Virus Protection and more available at http://www.plus.net
>      >_______________________________________________
>      >Javascript mailing list
>      >Javascript at LaTech.edu
>      >https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 
> --
> This email has been verified as Virus free
> Virus Protection and more available at http://www.plus.net
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> --
> This email has been verified as Virus free
> Virus Protection and more available at http://www.plus.net



More information about the Javascript mailing list