Hacker News :
Home » , » DISABLE TEXT SELECTION AND RIGHT CLICK USING JAVA SCRIPT CODE

DISABLE TEXT SELECTION AND RIGHT CLICK USING JAVA SCRIPT CODE

Written By Dipanshu Garg on Saturday, 26 January 2013 | 01:30

You created a nice website or a blog by working on it for hours, but did you thought that what if somebody copies your content? At least I thought about it when I created this blog. 



I searched for this on Google and found JavaScript code to do this. But after lot of thinking, I decided not to do that as this blog has a category of programming which includes lot of coding & if the text selection and right click was disabled no one would type all that code themselves and as a result they might stop visiting this blog! Anyways if you want to disable right click and text selection, u can follow the follow the following instructions.

I found many scripts to do this but most of them were cross browser incompatible. Disabling the right click works on all browsers, but the text selection blocking javascript code in mozilla prevents click on the textboxes. Means if you have blocked the text selection using the javascript you, will not be able to click on the search box, contact form textbox etc.

When I faced this issue, I removed some events from javascript and used css to make it working. Setting the -moz-user-select property to none, disables the text selection in Mozilla Firefox.

STEPS:-
1) Copy the code given below and paste it below the head tag of html page on which you want to disable right click and text selection.







<script language="JavaScript1.2" type="text/javascript">
  //The functions disableselect() and reEnable() are used to return the status of events.
        function disableselect(e)
        {
                return false 
        }
         function reEnable()
        {
                return true
        }
        //if IE4 
        // disable text selection
        document.onselectstart=new Function ("return false")
        //if NS6
        if (window.sidebar)
        {
                //document.onmousedown=disableselect
                // the above line creates issues in mozilla so keep it commented.
                document.onclick=reEnable
        }
        function clickIE()
        {
                if (document.all)
                {
                        (message);
                        return false;
                }
        }
        // disable right click
        document.oncontextmenu=new Function("return false")
</script>

2) Add the below code into your CSS :

body
{
-moz-user-select: none;
}


Note: This code works with html, asp.net, php and all other languages as it's using just javascript and css. Code is tested and working in below browsers:
  1. Internet explorer (all versions)
  2. Mozilla
  3. Chrome
  4. Safari
  5. Epic

If you are using Blogger platform like me then it would be really easy to perform this task as you just need to edit the template once and the changes will be reflected in all the pages.

Share this article :

0 comments:

Speak up your mind

Tell us what you're thinking... !

Note: only a member of this blog may post a comment.

Popular Posts