HTML输入密码后才能访问

时间:2022-10-27
本文章向大家介绍HTML输入密码后才能访问,主要内容包括前言、实现、逼格稍微高点的页面密码框、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

前言

有小伙伴提了个需求:说网站放 Github Page 上,但不想人人都能访问,怎样用做一个密码加密,比如输入6位密码,密码输入正确自动跳转,错误给出提示?

实现

这个可以用HTML加JS写一个密码输入框,在正确输入密码前阻断内容显示,糊弄下小白还是可以的,画面也比较简洁。

简单实用密码弹出框

在模板或者HTML页头位置加入如下代码,只要是内容显示前就行:

<script language = JavaScript>
function password() {
	var testV = 1;
	var pass1 = prompt('请输入密码:', '');
	while (testV < 3) {
		if (!pass1) history.go(-1);
		if (pass1 == "123456") {
			alert('密码正确!');
			break;
		}
		testV += -1;
		var pass1 = prompt('密码错误!请重新输入:');
	}
	if (pass1 != "password" & testV == 3) history.go(-1);
	return " ";
}
document.write(password());
</script>

逼格稍微高点的页面密码框

body里面的代码

<span style="font-family: 黑体;">请输入密码:</span><input id="password" type="password" name="密码输入框"/>
<table width="300" height="300" style="text-align: center;">
	<tr>
		<td id="1" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">1</td>
		<td id="2" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">2</td>
		<td id="3" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">3</td>
	</tr>
	<tr>
		<td id="4" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">4</td>
		<td id="5" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">5</td>
		<td id="6" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">6</td>
	</tr>
	<tr>
		<td id="7" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">7</td>
		<td id="8" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">8</td>
		<td id="9" onmouseenter="doenter(this)" onmouseout="doout(this)" onmousedown="dodown(this)" onmouseup="doup(this)">9</td>
	</tr>
</table>

js代码

var password = 123456;
var input = document.getElementById("password");
var td = document.getElementsByTagName("td");
for(var i = 0;i < td.length;i++){
	td[i].style.color = "white"
	td[i].style.backgroundColor = "#EAEAEA";
	td[i].style.borderRadius = "50px";
}
function dodown(obj){
	obj.style.backgroundColor = "darkgray";
	input.value += obj.id;
	
}
function doup(obj){
	obj.style.backgroundColor = "#EAEAEA";
	if(input.value.length == 6){
		if(input.value == password){
			alert("登录成功");
			location.href="https://sobaigu.com";
		}else{
			alert("密码错误,登录失败")
			input.value = null;
		}
	}
}
function doenter(obj){
	obj.style.backgroundColor = "lightgray";
}
function doout(obj){
	obj.style.backgroundColor = "#EAEAEA";
}