SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disable rightclick menu in Chrome?

    Hi all,

    I am working on an existing page for a client, he wants right click disabled in Chrome and Safari (dont ask!). The existing Javascript code works to disable the rightclick / menu in IE and FF, but not Chrome and Safari.

    Could anyone tell me how, if at all posible can I disable the right click menu in Chrome? Any help MUCH appreciated, im on a deadline to fix this.

    Kind regards and thankyou


    Existing Code (not my code):

    Code JavaScript:
    	<!-- Copyright Alert Function -->
    		function click(e){
    			var message="You cannot do that";
    			if (document.all){ // for IE
    				alert("IE")
    				if (event.button==2||event.button==3){
    				alert(message);
    				return false;
    				}
    			} else { // for Firefox
    				if (e.button==2||e.button==3){
    					alert("FF")
    					e.preventDefault();
    					e.stopPropagation();
    					alert(message);
    					return false;
    				} else {
    					alert("Chrome / Safari");
    					}
    				}
    			}
    		}
    		if (document.all){ // for IE
    			document.onmousedown = click;
    		} else { //for FF
    			document.onclick = click;
    		}

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    I have to head off, but briefly, have you considered performing some research with a google search? You can nicely narrow your options by using the right keywords.

    "google chrome" javascript disable right-click
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    187
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I'm also limited with time, just a suggestion - do it with jQuery. You'll save time.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Perhaps those browsers default to disabling the ability of web pages to do that. Most modern browsers at least allow the browser owner to disallow web pages ability to interfere with the operation of their operating system in that way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here is a way to examine all mouse click events. It works in all browsers, both IE and non-IE. The only problem I found was the wheel click event in Opera - it doesn't show the button integer. Try right, middle and left clicking on the page in all of your browsers.

    I tested this in Chrome 8.0.552.237, Firefox 3.6.13, Opera Ver:11, Safari 5.0.3 and IE 5.5, 6, 7, and 8.

    Code :
    <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     
    <html>
     
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Mouse click events</title>
    <script type="text/javascript">
    <!--
    // examines browser mouse click events
            function click(evt)
            { // rationalise event syntax 
                var e=(evt)?evt :window.event;
                var message="You cannot do that";
               //  test for IE         
                if(typeof e.which=="undefined")
                { // right click event
                   if(e.button==2)
                     { alert(message+" IE= "+e.button);    
                        return false;
                       // ---------                
                      } 
                   else 
                     { // other types of events  
                       alert("IE= "+e.button);
                     }                        
                } 
               else 
                { // for other browsers
                      if(e.which==3)
                       { alert(message+" not IE= "+e.which); 
                         e.preventDefault();
                         e.stopPropagation();               
                         return false;
                       // ----------- 
                       } 
                     else
                       { // other types of events  
                         alert("not IE= "+e.which);
                       } 
                  }                  
             } 
    // ----------
      function init()        
        { document.onmousedown=click;     
          document.oncontextmenu=new Function("return false")        
         }        
    // -----------
     window.onload=function (){ document.onmousedown=click; document.oncontextmenu=new Function("return false");  }       
     
     //-->
    </script>
    </head>
     
    <body onload="init()">
     
    <div id="test">
      Click on page to see event alert</div>
    <!-- end test div -->
     
    </body>
     
    </html>

  6. #6
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou very much all for your kind help, and great script Allan thanks

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AllanP View Post
    Here is a way to examine all mouse click events. It works in all browsers, both IE and non-IE.
    Except it can't be used to block the right mouse button when the browser is set to disallow blocking the right mouse button - when the browser is set to disallow blocking that the normal right mouse button functioning occurs before the click is passed to the web page and may not even be passed to the web page depending on the browser.

    Anyway that code does not block the context menu being accessed from the menu key on the keyboard (to the left of the right CTRL key) and so cannot prevent access to the menu even if the browser does allow the mouse button to be blocked as all the person would need to do to get around that their right mouse button is blocked is to give the desired field the focus (tabbing to it using the keyboard is the easiest way) and then hit the menu button.

    Alternatively if their browser doesn't allow them to override blocking that mouse button and they don't want to switch to a browser that does they can easily add a bookmark script that they can click on to restore the normal functioning of the right mouse button if the page blocked it. eg.http://javascript.about.com/library/blright.htm

    Of course another option they could use is to simply turn JavaScript off for the web page. JavaScript is a web page is only useful when it makes using the page easier for the person visiting and blocking the functioning of their browser does not do that so they will obviously just turn it off one way or another.

    The only people you can block from using their right mouse button are newbies who haven't yet discovered how easily such blocks can be overcome.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think most of us realise that you can't really block people who are intent upon copying the page content. However, it does slow down the honest people and, given the number of beginners asking how to do this, it is still somewhat of a mystery to many people. In summary, worth doing, but offering low security.

  9. #9
    38911 Basic Bytes Free johnuk's Avatar
    Join Date
    Jul 2008
    Location
    Somerset, England
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I certainly wouldnt do it myself... but you cant argue with a client, even if you give a firm recomendation against it

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by johnuk View Post
    Well, I certainly wouldnt do it myself... but you cant argue with a client, even if you give a firm recomendation against it
    You really should argue with the client on that one - after all you are the professional and they will lose a lot of legitimate visitors if they insist on trying to do that without it actually doing anything with respect to the reason why they want it there. If anything adding a script to try to block the right mouse button will increase the number of people stealing the page content just because they think it is somehow clever that they know how to bypass that block.

    Ask them if that is what they really want to achieve as that is all that blocking the right mouse button does.

    If they really do want to encourage people to steal their content and to drive away legitimate visitors then you should proceed to implement what they have asked for as then the results of doing so will match their expectations.

    If they don't want to drive away legitimate visitors and increase the amount of content theft then you need to ask them why they have asked you to implement something that does that instead of implementing something that achieves what they actually want.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •