Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Protect your content

Although there is no way to completely protect your content, atleast you can do something to make it harder. This restrictions are sometimes mandatory for different websites, so here are the tips how you can protect your content using javascript.

So it is possible to:

  • disbale right click of the mouse and context menu
  • disable text selection
  • hide content from printing
<html>
<head>
</head>
<body>
<!-- some test content -->
<p>Some content utt.</p>
<p>text and <input type='text' value='input'/></p>
<p>text and <a href='#link'>Link</a></p>
<p><textarea rows='10' cols='10'></textarea></p>
<script>
    /**********************
    * SOME COMMON FUNCTIONS
    **********************/
    //prevent default event behaviour
    var prevent_default = function(event){
        event = (event) ? event : window.event;
        if(event.preventDefault)
        {
            event.preventDefault();
        }
        else
        {
            event.returnValue = false;
        }
    };
    //add event handler
    var add_event = function(element, type, listener){
        if(element.addEventListener)
        {
            element.addEventListener(type, listener, false);
        }
        else
        {
            element.attachEvent('on' +  type, listener);
        }   
    };
    /**********************
    * PROTECTING CONTENT
    **********************/
    //disable right click
    var disable_click = function(){
        //check if right mouse button was pressed
        var check_button = function(e) {
            var rightclick = false;
            if (!e) var e = window.event;
            if (e.which) rightclick = (e.which == 3);
            else if (e.button) rightclick = (e.button == 2);
            return rightclick;
        };
        //disable right button
        var disable = function(e){
            if(check_button(e))
            {
                prevent_default(e);
            }
        };
        //add it to events
        add_event(document, "mouseup", function(e){
            disable(e); 
            return false;
        });
        add_event(document, "mousedown", function(e){
            disable(e); 
            return false;
        });
        add_event(document, "contextmenu", function(e){
            disable(e); 
            return false;
        });
    };
    //disable text selection
    var disable_select = function(){
        //for IE
        add_event(document, "selectstart", function(e)
        {
            prevent_default(e); return false;
        });
        //for Opera
        if(window.opera)
        {
            //set unselectable = "on" to every tag on page
            document.body.unselectable="on";
            var tags = document.body.getElementsByTagName("*");
            for(var i = 0; i < tags.length; i++)
            {
                tags[i].unselectable="on";
            }
        }
        //Others browsers
        document.body.style.MozUserSelect="none";
        document.body.style.KhtmlUserSelect="none";
        document.body.style.WebkitUserSelect="none";
        document.body.style.userSelect="none";
    };
    //disable page printing
    var disable_print = function(){
        var head = document.getElementsByTagName('head')[0];
        //create style for print media and hide body
        var newStyle = document.createElement('style');
        newStyle.setAttribute('type', 'text/css');
        newStyle.setAttribute('media', 'print');
        newStyle.appendChild(document.createTextNode('body {display: none;}'));
        head.appendChild(newStyle);
    }
    disable_click();
    disable_select();
    disable_print();
</script>
</body>
</html>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus