Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disable right click 2

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
Hi I've got the following code that I believe will disable the right click on a html page
Code:
  <SCRIPT LANGUAGE="JavaScript">
    <!--
      var message="Right Click Disabled"
      function click(event)
      {
        if (document.all)
        {
          if (event.button == 2)
          {
            alert(message);
            return false;
          }
        }
        if (document.layers)
        {
          if (event.which == 3)
          {
            alert(message);
            return false;
        }
      }
    } 

    if (document.layers)
    {
      document.captureEvents(Event.MOUSEDOWN);
    }

    document.onmousedown=click;
    //-->
  </SCRIPT>
If I put this in the <head> section of my file then it works for that page.

My page is build up with frames, can I put this in my frames.html so that the rule is valid for all child.html pages???

I do not want to have to put it in all my child html. Also in some frames I will load up a picture and not a html so how can I disable the right click in that instance?

Thanks

 
you shouldnt load an image directly into the browser, always embed in a html file.

some browsers dont know what to do with images without html so they attempt to download it instead of displaying.



I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
I got this to work for me in IE6 (thanks to simonchristieis at this post and my own knowledge on the subject:

Code:
<html>
<head>
<script>
function stopRightClick()
{
 document.oncontextmenu = function(){return false}
 pictureFrame.document.oncontextmenu = function(){return false}
}
</script>
</head>
<body onload='stopRightClick()'>
Hello, World!
<iframe width='100%' height='100%' id='pictureFrame' src='Nic1.bmp'>
</iframe>
</body>
</html>

You'll notice that the function disables the right-click in both the parent document and the picture-only child document in the iframe ("oncontextmenu" means "onrightclick").

So it seems like you can remotely set this property in child frames. I didn't try it with a frameset, but my guess is you can make it work with one.

I probably wouldn't put it in a BODY tag's onload event, however, without putting some kind of delay mechanism in there to make sure the frame you're aiming for is loaded in time for the remote event to be added. I imagine an error would result if (in my code above, for example) iframe pictureFrame didn't finish loading before the javascript line pictureFrame.document.oncontextmenu = function(){return false} tried to run.

To solve K9logic's concern above, you could do the following with a picture frame:

Code:
<html>
<head>
<script>
function loadIframeAndStopRightClick()
{
 document.oncontextmenu = function(){return false}
 pictureFrame.document.open();
 pictureFrame.document.write("<html><body oncontextmenu='return false;'><img src='Nic1.bmp' /></body></html>");
 pictureFrame.document.close();
}
</script>
</head>
<body onload='loadIframeAndStopRightClick()'>
Hello, World!
<iframe width='100%' height='100%' id='pictureFrame' src='Nic1.bmp'>
</iframe>
</body>
</html>

Notice, in this case, the oncontextmenu event is written right into the BODY tag of the iframe image's enveloping HTML document.

'hope this helps.

--Dave
 
thats a pretty clever solution for the img thing :) didnt think of that. is that crossbrowser compatible?

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
'afraid I don't know about the cross-browser thing. We only use IE6 at work. I don't have other browsers available.

--Dave
 
thanks for all the help. It "almost" works the way I require now!!

One final thing is the right click is now disabled (as required) but when the user "hovers" over a picture then they still have the option to save the picture in the top left corner??

Any ideas how I can get rid of this?
 
or put
Code:
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
in the head to disable all image download toolbar thingy popups

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top