Simple Password Protection
What if you want to add a simple password to a webpage? Of course, the right way to do it involves having a database-driven site and other resources simple nonprofit sites don't have. How can a password be added to a webpage in the easiest way possible?
Easiest Way
The easiest way is to just add this following code to your webpage in the head section. Copy this following code and open up the webpage you want to put a password on and paste this code after the <head> tag and before the </head> tag.
<script type="text/javascript">
<!-- Begin
var password;
var pass1 = "password"; // place password here
password=prompt("Please enter your password:","");
if (password==pass1) {
window.location= "simplepassword3.html"; // file to open if password
is correct
} else {
window.location= "simplepassword4.html"; // file to load if password
is incorrect
}
// End -->
</script>
Now, where it says var pass1="password" above, replace the word "password" with the password you really want.
Now, where it says "test2.html" put the name of the webpage you want to open up if they put in the correct password.
Similarly, where it says "test1.html" put the name of the webpage you want to open up if people put in the wrong password.
Lastly, save all these files in the same folder/directory and test them out. It should work easily.
Problems?
Anyone familiar with webpages who doesn't know the password can go VIEW --> SOURCE and see the password and the URL of the page one goes to after putting in the password correctly. Also, after putting in their password the name of the page is displayed on top and people can tell all their friends who may not know the password. So, this is not very secure at all, but better than nothing. At the very least, it is a good exercise in beginning JavaScript. And if you want more secure scripts check out the following.
This script is courtesy of the nice folks at The JavaScript Source :: http://javascript.internet.com.