tags:

views:

184

answers:

12

Hello any one can help a beginner please. the code is for adding a password on the webpage, which working for all other browser but not for IE.

<HEAD>
<SCRIPT language="JavaScript">
<!--hide

var password;

var pass1="cool";

password=prompt('Please enter your password to view this page!',' ');

if (password==pass1)
alert('Password Correct! Click OK to enter!');
else
{
window.location="http://www.pageresource.com/jscript/jpass.htm";
}

//-->
</SCRIPT>
</HEAD>
A: 

it looks like your first line of js:

<!--hide

should be :

// <!-- hide

But it looks liek you may have other problems as well. Try that first!

Joseph Silvashy
For the record, you *do not* want to use this method to comment out scripts. Here's an article as to why: http://lachy.id.au/log/2005/05/script-comments
JasCav
agreed, but it was a syntax flaw, so I offered a correction.
Joseph Silvashy
Nice link jason!!
Ambrosia
A: 

Try wrapping curly braces around your if:

if (password==pass1){
    alert('Password Correct! Click OK to enter!');
} else {
    window.location="http://www.pageresource.com/jscript/jpass.htm";
}

You probably don't really need that hide stuff anymore either. Good luck on your learning.

Jage
+4  A: 

window.location doesn't work in IE6. You probably want document.location.

if ("cool" == prompt('Please enter your password to view this page!', '')) {
    alert('Password Correct! Click OK to enter!');
} else {
    document.location = "http://www.pageresource.com/jscript/jpass.htm";
}
Michiel Kalkman
+1, and true dat for mentioning _document_ vs. _window_ in IE
Joseph Silvashy
This is actually not true. It only fails to work in certain constructs, but not in the construct the OP is trying. Besides, the `document.location` has *more* cross-browser issues than `window.location`.
BalusC
+4  A: 

IE7 has the prompt functionality disabled by default.

And as everyone before me has said unless this is just a learning javascript type endevour - the "protection" is useless. All anyone has to do is disable JS and/or look at your source for where youre redirecting.

prodigitalson
+1 this is the reason it doesn't work. Although the other whinges about faux-security and dodgy JS practice are also true.
bobince
A: 

If my JavaScript only breaks in IE, the very first thing I do is run it through JSLint, a free tool that checks your JS code for errors. This will likely reveal what's causing IE to choke.

inkedmn
+1  A: 

Ok, firstly I hope you are just doing this to play with javascript because anyone will be able to bypass your security in a snap just by looking at the source code. To manage login access to a site you really should be using a server side implementation such as php, jsp, asp etc. Those sites take the password login and compare it on the server which the normal user cannot see. Anyway your code below should work:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>HTML Test</title>
        <script type="text/javascript">
            function check()
            {
                var password;
                var pass1="cool";
                password=prompt('Please enter your password to view this page!',' ');
                if (password==pass1)
                    alert('Password Correct! Click OK to enter!');
                else
                    document.location="http://www.pageresource.com/jscript/jpass.htm";
            }
        </script>
    </head>
    <body onload="check()">
    </body>
</html>
Ambrosia
A: 

DON'T DO THAT!!! You're sending the user the password. All the user has to do is "View Source" and it's right there. Alternatively, the user can get whatever information the password was hiding from the Javascript source.

Client-side validation is great for things like data formats, but nothing that is sensitive should ever be done client-side, and all data should be validated again on the server. Remember, a malicious user can send you anything, bypassing your Javascript entirely.

David Thornley
Maybe he is making 101 hacking challenge: level 1 :)
Jay Zeng
A: 

its this bit of code here

if (password==pass1)
alert('Password Correct! Click OK to enter!');
else
{
window.location="http://www.pageresource.com/jscript/jpass.htm";
}

first of all wrap the alert bit in curly braces to read

if (password==pass1){
  alert('Password Correct! Click OK to enter!');
}

secondly, unlike php and other C like languages, do not start braces in a new line...
in javascript, when a script is evaluated by the interpreter, it looks for a semicolon designating the end of a statement,
if a semicolon is not found, the closest previous line break is replaced with a semicolon

So, your else block is evaluated as :

else;
{;
window.location="http://www.pageresource.com/jscript/jpass.htm";
};

instead write it as

else{
  window.location="http://www.pageresource.com/jscript/jpass.htm";
}
pǝlɐɥʞ
A: 

When I used the IETester's "open URL in all IE versions", it failed to work in all windows expect of the first window where the prompt was entered, regardless of the IE window used.

However, it works fine here in all known browsers (including IE6/7/8) when tested individually. I haven't change anything from the code as outlined here.

So your actual problem lies somewhere else than in the as far provided information.

Needless to say, this approach is far from secure.

BalusC
A: 
  1. I think you are missing one squiggly at end of 'if' line, and another at beginning of 'else' line, or, remove the two that are existing now .

So, either this:

if (password==pass1) {
 alert('Password Correct! Click OK to enter!');
} else {
 window.location="http://www.pageresource.com/jscript/jpass.htm";
}

.. or this (although I never code this way personally) ..

if (password==pass1)
 alert('Password Correct! Click OK to enter!');
else
 window.location="http://www.pageresource.com/jscript/jpass.htm";

should work.

Also, try window.location.href instead of just window.location.

window.location.href = "http://www.pageresource.com/jscript/jpass.htm";

Of course, as mentioned ny others, your approach is not secure using password on client side (<!--hide does NOT hide the javascript from the source code view).

Hope this helps -

OneNerd
A: 

FROM window.location TO document.location is all you need to change for a bug-free cross-browser implementation of your code. Never use window unless you are truly referencing the window object; Example: window.close()

Michael Mad
A: 

Hi everyone, many thanks for all your advices.

follow every single sugguestion, I change my code to:

var password;

var pass1="cool";

password=prompt('Please enter your password to view this page!',' ');

if (password==pass1){ alert('Password Correct! Click OK to enter!'); } else { document.location.href="test2.html";

}

However still no luck to make it work in IE. someone said the prompt script got problem. but I don't know what else I can use.

as this script is just for normal protection for webpage, not for high security stuff. so its fine even some people can find out from code source.

could any one give me more advice to make it work in IE, as I already worked on this issue for three days.

Many thanks!!