公司做的项目有的需要发布到网上,但是内容又不想被复制,那么久可以加上这样的代码
第一种
1
<script language=javascript type=text/javascript>
<!--
document.οncοntextmenu=new Function('event.returnValue=false;');
document.onselectstart=new Function('event.returnValue=false;');
-->
</script>
2 或者 在中加入以下代码:
<body oncontextmenu="return false" onselectstart="return false">
或
<body oncontextmenu="event.returnValue=false" onselectstart="event.returnValue=false">
实质上,这两个方法是一样的。
3 如果只限制复制,可以在加入以下代码:
<body oncopy="alert('对不起,禁止复制!');return false;">
4 使菜单"文件"-"另存为"失效
如果只是禁止了右键和选择复制,那么别人还可以通过浏览器菜单中的"文件"-"另存为"拷贝文件。为了使拷贝这个功能也失效,我们可以在与之间加入以下代码:
<body>
<noscript>
<iframe src="*.htm"></iframe>
</noscript>
</body>
这样,用户在另存网页时,就会出现"无法保存Web页"的错误。
5 也可以使用event.preventDefault() 方法来阻止oncontextmenu() ,
还有onselectstart()
document.oncontextmenu=function(evt){
evt.preventDefault();
}
document.onselectstart=function(evt){
evt.preventDefault();
};
6 既然可以禁止,那么当然也可以启用它,将事件重新赋值即可,可以赋值为null,或字符串、布尔值都行。如:
document.oncontextmenu="";
document.onselectstart=true;
第二种
1. 屏蔽选中
<script>
document.onselectstart = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>
2. 屏蔽复制
<script>
document.oncopy = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>
3. 屏蔽剪切
<script>
document.oncut = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
4. 屏蔽粘贴
<script>
document.onpaste = function (event) {
if (window.event) {
event = window.event;
}
try {
var the = event.srcElement;
if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")) {
return false;
}
return true;
} catch (e) {
return false;
}
}
</script>