当前位置:
首页 > 技术 > JS+CSS 防止复制+禁止右键 兼容主流浏览器

JS+CSS 防止复制+禁止右键 兼容主流浏览器

很多时候我们自己网站上的原创文章不希望被别人直接复制拷贝走

下面我就给大家介绍一个JS,可以实现:防止复制+禁止右键的效果,并且兼容目前的各个浏览器。

CSS:

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

Javascript:

function iEsc() {
    return false;
}
function iRec() {
    return true;
}
function DisableKeys() {
    if (event.ctrlKey || event.shiftKey || event.altKey) {
        window.event.returnValue = false;
        iEsc();
    }
}
document.ondragstart = iEsc;
document.onkeydown = DisableKeys;
document.oncontextmenu = iEsc;
if (typeof document.onselectstart != "undefined") {
    document.onselectstart = iEsc;
}else{
    document.onmousedown = iEsc;
    document.onmouseup = iRec;
}
function DisableRightClick(val) {
    if (window.Event) {
        if (val.which == 2 || val.which == 3) iEsc();
    } else if (event.button == 2 || event.button == 3) {
        event.cancelBubble = true;
        event.returnValue = false;
        iEsc();
    }
}

网友评论3

  1. 板凳
    蒂欧娜:

    您的博客拥有旺盛的生命力!!

    2016-07-20 5:31 PM
    • Travon:

      It's great to find an expert who can exalpin things so well

      2016-08-14 12:19 PM
  2. 沙发
    Gump:

    that, let me inform you prseciely what did deliver the results. Your writing is certainly really engaging which is possibly why I am making an effort to opine. I do not make it a regular habit of doing that. Secondly, although I can notice the leaps in reason you come up with, I am not certain of just how you appear to connect the details that produce the final result. For right now I shall subscribe to your position however hope in the future you link your facts better.

    2016-08-14 11:03 AM

发表评论

您必须 [ 登录 ] 才能发表留言!