js方法禁止查看源文件、防止复制、禁止右键、防被框架的方法总结

本文介绍了一些基本的网页防复制、防另存及禁止查看源代码的方法,包括使用JavaScript禁用鼠标右键、阻止文本选择等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一些禁止查看源文件的脚本,用在网页中,能起到防止别人复制等效果,但是也可以通过其他办法看到源代码,或是复制里面的内容。下面的方法都是一些基本的js脚本,简单的用用就行了,真正能防到什么程序,谁也不好说。有句话说的好,真正做的好的页面,就不怕被人仿、复制、看,本来互联网就是个开放的嘛。
锁定右键
将网页中的换为如下代码即可:

<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()">

拒绝另存
在和之间加入如下代码:

<noscript><iframe src="/*>";</iframe></noscript>


禁止查看源代码
尽管通过以上方式屏蔽了右键查看源文件,拒绝了复制和另存,但通过工具栏的【查看】-【源文件】仍会使网页失锁!
要彻底禁止查看源文件,可通过零框架技术解决——将页面分为左右两帧,左帧的宽度为0,载入空页面文件“null.htm”;右帧载入要保护代码的页面,框架面页为index.htm,代码如下:

<html> 
<head> <title>欢迎光临--笑申博客--</title> </head> 
<frameset cols="0,*" frameborder="NO" border="0" framespacing="0">
<frame src="/null.htm" name="leftFrame" scrolling="NO" noresize>
<frame src="/index.html" name="mainFrame">
</frameset><noframes></noframes>
</html> 


同时,已被锁定右键、禁止另存的主面页(原代码见上)命名为 “index.html”,再添如下代码,以防被独立打开:

<script language="javascript"> if(top==self)top.location="index.html" </script>


防止被别人框架

<SCRIPT language=javascript>
if (top.location != self.location){top.location=self.location; }
</SCRIPT>

 
取消选取、防止复制 

<SCRIPT language=JavaScript>
window.ClearEvent=function(){event.cancelBubble=false;var sSrcTagName=event.srcElement.tagName.toLowerCase();
return (sSrcTagName=="textarea" || sSrcTagName=="input" || sSrcTagName=="select");}
window.ClearKey=function(){event.cancelBubble=false;var iKeyCode=event.keyCode;return !(iKeyCode==78 && event.ctrlKey);}
with (window.document){oncontextmenu=onselectstart=ondragstart=window.ClearEvent;onkeydown=window.ClearKey;}
</SCRIPT>
<BODY oncontextmenu=self.event.returnValue=false onselectstart="return false">
<body onselectstart="return false">


不准粘贴

onpaste="return false" 


防止复制 

oncopy="return false;" oncut="return false;"


关闭输入法

<input style="ime-mode:disabled">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值