Simple Scripts to Password Protect Webpages

Here are some simple password protection scripts to password protect webpages. Some web pages are best kept private. The javascripts below are simple ways to protect using a password your webpage.


password protection scripts

  • As I said, these scripts are very simple javascript. They are the first line to protect privacy of webpages. Someone who can access the html page source code can easily find the username and password and enter the page. Anyone with a little idea about html and computers can see the source code.
  • The script will NOT work in Javascript disabled browsers or older browser versions not supporting javascript. So it is basically to stop routine surfers from accessing your pages.

So if you have something IMPORTANT that you really want to password protect and not allow any access at all, DO NOT USE these and try alternative means.

USAGE – Insert the code in between the HEAD html  tags of your webpage. Remember to replace our url ‘’ with the url of your webpage.

Password Protection Script 1

The javascript displays a form to enter the Username and Password. If either is wrong, it will display and alert screen like ‘Wrong Username’ or ‘Wrong Password’. The username is set to ‘secretname’ and the password is ‘secretpassword’ – both can be set as you like. If both are right, then you are taken to the next url, which is currently set to this blog.

<script language="javascript">
function pasuser(form) { 
if ("secretname") { 
if (form.pass.value=="secretpassword") { location="" } 
else { alert("Wrong Password") } 
else { alert("Wrong Username") } 
<form name="login">
Username: < input name="id" size="6" type="text">
Password: < input name="pass" size="6" type="password">
<input value="Login" onclick="pasuser(this.form)" type="button">

Password Protection Script 2

Insert this code in the ‘Head’ tags of the web page. When you enter the page, it will ask for a password, currently set to ‘secretpassword’. If the password is correct, you will be allowed to enter the page. Otherwise you can set it to redirect to any other page, currently it is set to this blog.

<SCRIPT language="JavaScript">
var password; 
var pass1="secretpassword"; 
password=prompt('Enter Password',' '); 
if (password==pass1) 
alert('Correct Password! Click OK to Enter!'); 
else { window.location=""; 

Insert these scripts into your html code for simple password protection of your pages. But remember the warning as mentioned before.