两种方法实现html页面加密访问。
只加密html
此方法屏蔽F12查看源码但是屏蔽不了Ctrl+U查看源码。将下面代码在</head>
前面加入即可。
<SCRIPT language=JavaScript>
<strong>function</strong> <strong>password</strong>() {
<strong>var</strong> testV = 1;
<strong>var</strong> pass1 = <strong>prompt</strong>('请输入密码:','');
<strong>while</strong> (testV < 3) {
<strong>if</strong> (!pass1)
history.<strong>go</strong>(-1);
<strong>if</strong> (pass1 == "123") {
<strong>alert</strong>('密码正确!');
<strong>break</strong>;
}
testV+=-1;
<strong>var</strong> pass1 = <strong>prompt</strong>('密码错误!请重新输入:');
}
<strong>if</strong> (pass1!="password" & testV ==3)
history.<strong>go</strong>(-1);
<strong>return</strong> " ";
}
document.<strong>write</strong>(<strong>password</strong>());
</SCRIPT>
加密html和js
html代码
<script src="psw.js"></script><!--密码定义在 psw.js 里面-->
<script>
<strong>function</strong> <strong>password</strong>() {
<strong>var</strong> testV = 1;//计数器,记录密码错误次数
<strong>var</strong> psw = <strong>prompt</strong>("请输入密码:","");
<strong>while</strong> (testV < 3) {
<strong>if</strong> (!psw){
history.<strong>go</strong>(-1);//密码错误返回上一页
}
<strong>if</strong> (psw == xx) {
location.href="index2.html";//密码正确,跳到指定页面
<strong>break</strong>;
}
testV+=1;
psw=<strong>prompt</strong>("请输入密码:","");
}
<strong>if</strong> (psw!=xx && testV ==3){
<strong>alert</strong>("错错错!!!,请刷新重新输入密码");//密码输入错误达到3次
}
<strong>return</strong> " ";
}
<strong>password</strong>();
</script>
JS代码
// JavaScript Document
<strong>var</strong> xx="666666";//这是进站密码