I need to tack some simple password protection onto arbitrary html files. It does need need to be secure, but just keep out the random roff-raff. I tried using the below JS:
var password;
var thePassword="secretpassword";
password=prompt('Enter Password',' ');
if (password==thePassword) { alert('Correct Password! Click OK to Enter!'); }
else { window.location="http://www.google.com/"; }
This works fine in Firefox, but seems that the prompt function fails in IE and so we always redirect to google...
Any suggestions on how to do a simple password protection using straight HTML pages?
EDIT: to be clear, it works fine in Firefox, and in IE does not even prompt with a popup asking to "Enter Password"
-
The code seems to work fine for me on IE9. I just ran it in this jsfiddle and it works: jsfiddle.net/jfriend00/KkMRS. What version of IE are you running? I'd suggest putting an alert on what the actual value is coming back from the prompt.jfriend00– jfriend002011年06月30日 22:52:35 +00:00Commented Jun 30, 2011 at 22:52
2 Answers 2
Works fine for me in IE. Demo: http://jsfiddle.net/U2n3P/
One possible reason it may not be working is that you're populating the prompt with an empty space, by using ' ', so when you start typing there may be a space at the end. Change the prompt to:
password=prompt('Enter Password', '');
FYI, I know you said you didn't need this to be super secure, but you might as well add some security. Get an MD5 library and do, instead:
var thePassword = "md5encodedpassword";
password=prompt('Enter Password',' ');
if(md5(password) != thePassword){
3 Comments
Name the secure page or directory the md5 hashed password.
function isThere(url) {
var req= new AJ(); // XMLHttpRequest object
try {
req.open("HEAD", url, false);
req.send(null);
return req.status== 200 ? true : false;
}
catch (er) {
return false;
}
}
password=prompt('Enter Password',' ');
password=md5(password);
if (isThere(md5 + "/") { window.location = password + "/"; }
else { alert("incorrect"); }
1 Comment
Explore related questions
See similar questions with these tags.