Password protect a web page using Javascript

By: Guru Singh Emailed: 1587 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In the below code, "Password" is the password and "protected.html" is the name of the password-protected page. (Substitute your own password and URL when you use this script.) If visitors enter the correct password, they will forward to "protected.html"; if they enter an incorrect password, they'll hit the page called "oops.html." 

This shouldn't be used as a substitute for true security, but it isn't bad in a pinch. It works nicely for, say, an area of your site reserved for family members only. 

If you do use this code to protect a page or directory, you might want to keep search engines from listing it. Place this tag in the HEAD section of your protected page: 

<meta name="robots" contents="noindex"> 

Of course, it's possible for someone clever enough to get the password right from the source code. So you should somehow make the 'View Source' option impossible. You can do so by disabling Right click using Javascript.

<script language="Javascript">
<!--
var password = prompt("Enter in the password")
if (password == "Password") {
alert("Welcome!")
location = "protected.html"
}
else {
location = "oops.html"
}
//-->
</script> 

 


Javascript Home | All Javascript Tutorials | Latest Javascript Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Hi there,
this doesnt work in IE7 or firefox?
If you disable the right click option, the user can still access the source code by going to View==>Page Source

Am I doing something wrong~?


View Tutorial          By: Gerrard at 2009-10-17 15:19:24
2. View Comment

I used the script above, and it seems to work except for 1 problem...when I type in the password, a welcome screen comes up, when you press 'ok' it goes back to the 'enter password' window. How do I fix this?

View Tutorial          By: Michael at 2012-05-15 20:50:49

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Guru Singh
Scrolling message on the status bar using Javascript
Diabling Right Click option in a browser using Javascript
Password protect a web page using Javascript
Using revealTrans to do page transitions in Javascript
Question: How is the MVC design pattern used in Struts framework?
Question: What is the directory structure of a web application?
Question: Explain the life-cycle methods in JSP?
Question: What is an output comment in JSP?
Question: What is a Hidden Comment in JSP?
Question: What is an Expression tag in JSP?
Question: Difference between forward and sendRedirect in JSP?

More Tutorials in Javascript
Use WinSCP to transfer log files remotely using Javascript
Verifying user input in JavaScript
Pause execution in JavaScript
Javascript to display client date and time on webpage
Getting Browser's height and width using Javascript
Highlighting text on a page using CSS
Using revealTrans to do page transitions in Javascript
Password protect a web page using Javascript
Diabling Right Click option in a browser using Javascript
Scrolling message on the status bar using Javascript
window.frames[i] in Javascript
Form validation using Javascript
Using && in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript

More Latest News
Most Viewed Articles (in Javascript )
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Typecasting in Javascript
List of Keywords and reverved words in Javascript
Scrolling message on the status bar using Javascript
Use WinSCP to transfer log files remotely using Javascript
this keyword sample in Javascript
call() and apply() methods in Javascript
Getting Browser's height and width using Javascript
Using toString() in JavaScript to convert data types to String
Bitwise NOT, AND, OR, XOR operators in Javascript
break and continue in Javascript
Pause execution in JavaScript
Click to remove an element from a select html form using javascript
History and evolution of Javascript
Most Emailed Articles (in Javascript)
Show how many characters remaining in a html text box using javascript
Bitwise NOT, AND, OR, XOR operators in Javascript
Click to remove an element from a select html form using javascript
List of Keywords and reverved words in Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Typecasting in Javascript
this keyword sample in Javascript
window.frames[i] in Javascript
The if statement in Javascript
do-while, while, for and for-in loops in Javascript
switch in Javascript
Math object and Math functions in Javascript
Form validation using Javascript
Highlighting text on a page using CSS
Diabling Right Click option in a browser using Javascript