网站内容保护代码

    选择打赏方式

温馨提示:
本文最后更新于2018年06月19日,已超过5年10个月(约2132天)没有更新,若内容或图片失效,请留言反馈。

如果我们想禁止用户复制网站文字内容或查看源代码等,我们可以通过以下方式来实现。

1.禁用选中和右键:

在<body>标签中添加以下代码:

oncontextmenu='return false'    禁止右键
ondragstart='return false'    禁止拖动
onselectstart ='return false'    禁止选中
onselect='document.selection.empty()'    禁止选中
oncopy='document.selection.empty()'    禁止复制
onbeforecopy='return false'    禁止复制
onmouseup='document.selection.empty()' 

如下:

<body leftmargin=0 topmargin=0 oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onselect='document.selection.empty()' oncopy='document.selection.empty()' onbeforecopy='return false' onmouseup='document.selection.empty()'>
2.禁止网页另存为:在<body>后面加入以下代码: 

<noscript> 
<iframe src="*.htm"></iframe> 
</noscript>  
这时在电脑端已经无法选择复制,但是在移动端还可以选中复制,再添加以下css代码用来禁止选中文字。

3.禁止选中文字

*{
moz-user-select: -moz-none;
-moz-user-select: none;
-o-user-select:none;
-khtml-user-select:none;
-webkit-user-select:none;
-ms-user-select:none;
user-select:none;
}
这时正常的选择复制都已经被禁用但是还可以用浏览器的查看源码和调试工具来直接从代码中复制内容。

4.禁用F12按键

//禁用F12
window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
// 判断是否按下F12,F12键码为123
if (event.keyCode == 123) {
event.preventDefault(); // 阻止默认事件行为
window.event.returnValue = false;
}
}
5.禁用调试工具

var threshold = 160; // 打开控制台的宽或高阈值
// 每秒检查一次
var check = setInterval(function() {
if (window.outerWidth - window.innerWidth > threshold ||
window.outerHeight - window.innerHeight > threshold) {
// 如果打开控制台,则刷新页面
window.location.reload();
}
}, 1000);

6.禁用CTRL+S 保存网页代码
function ctrl_key() {
//禁用CTRL+S 保存网页代码
if (event.keyCode == 17) {
window.alert("请尊重本网站版权!");
return false;
}
}
版权声明:若无特殊注明,本文为《寒星皓月》原创,转载请保留文章出处。
本文链接:https://www.wanghanyue.com/post-24.html
作品采用:知识共享署名 4.0 (CC BY-NC-SA 4.0) 国际许可协议 进行许可。
正文到此结束

相关推荐

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

呵呵 哈哈 羡慕 惊恐 超赞 嗯哼 滑稽 不高兴 大哭 疑问 你懂得 打脸 黑线 委屈 小乖 酷 笑眼 汗 what 鄙视 喷 阴险 怒 吃瓜

评论信息框
可使用QQ号实时获取昵称+头像

私密评论

吃奶的力气提交吐槽中...


既然没有吐槽,那就赶紧抢沙发吧!