Monday, April 9, 2012

JavaScript: protect web pages content

There's always an option to save the html/aspx/php page via browser options, however not every average user awares of this feature, so we at least can try and disable some basic web page features, including text selection and/or mouse right-click.



    <%--disables text select--%>
    <script type="text/javascript">
        function disableselect(e) {
            return false
        }
        function reEnable() {
            return true
        }
        //if IE4+
        document.onselectstart = new Function("return false")
        //if NS6
        if (window.sidebar) {
            document.onmousedown = disableselect
            document.onclick = reEnable
        }
    </script>

    <%--disables mouse right-click--%>
    <script type="text/javascript">
    var message = "Sorry, right-click has been disabled";

    function clickIE() { if (document.all) { (message); return false; } }
    function clickNS(e) {
        if
    (document.layers || (document.getElementById && !document.all)) {
            if (e.which == 2 || e.which == 3) { (message); return false; }
        }
    }
    if (document.layers)
    { document.captureEvents(Event.MOUSEDOWN); document.onmousedown = clickNS; }
    else { document.onmouseup = clickNS; document.oncontextmenu = clickIE; }
    document.oncontextmenu = new Function("return false")
    </script>

No comments:

Post a Comment