首页 > 技术文章 > Disable right click on the website

dragonlove 2014-04-19 16:13 原文

Many developers/website owners like to keep their website images personal and don't want anyone to copy it from their website.

In order to accomplish this place the following java script code in between the <head>  </head> tag of your master page:

<script type="text/javascript">

    var message = "Sorry, Right Clicks have been disabled"; // Message for the alert box

    // Don't edit below!

    function click(e) {
        if (document.all) {
            if (event.button == 2 || event.button == 3) {
                alert(message);
                return false;
            }
        }
        else {
            if (e.button == 2 || e.button == 3) {
                e.preventDefault();
                e.stopPropagation();
                alert(message);
                return false;
            }
        }
    }
    if (document.all) {
        document.onmousedown = click;
    }
    else {
        document.onclick = click;
    }
</script>

P.S. Just by disabling the right click doesn't stop anyone from downloading or copying from any website as there are many other ways to do that.

推荐阅读