Simple Javascript Question

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
I'm use the brute force method to bang myself through learning Javascript. Could somebody PLEASE tell me how the heck I load a page in the SAME window, instead of a new window?

This is the part of the code:

if (pass1 == "password") {
alert('Thank You');
window.open('protectedpage.html');
break;
}


So what do I use instead of window.open?
 

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
Originally posted by: Snapster
window.location.href = "protectedpage.html";

Doesn't work :(

It just accepts the password and keeps me at the same page I was at.
 

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
Here's the whole script that's in my header in case it helps:

<SCRIPT LANGUAGE="JavaScript">

function password() {
var testV = 1;
var pass1 = prompt('Please Enter Your Password','');
while (testV < 3) {
if (!pass1)
history.go(-1);
if (pass1 == "password") {
window.open('protectedpage.html');
break;
}
testV+=1;
var pass1 = prompt('Please Enter Your Password','');
prompt('Access Denied - Password Incorrect, Please Try Again.','Password');
}
if (pass1!="password" & testV ==3)
history.go(-1);
return " ";
}
// End -->
</SCRIPT>


-----


it's called with this in the body:

<FORM>
<input type="image" src="picture.jpg" onClick="password()">
</FORM>
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Originally posted by: Stangs55
Doesn't work :(

It just accepts the password and keeps me at the same page I was at.

It does work. :) Make sure you are doing:

window.location.href = 'protectedpage.html';

and not

window.location.href ('protectedpage.html');

Using your code as below...
 

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
Originally posted by: Snapster
Originally posted by: Stangs55
Doesn't work :(

It just accepts the password and keeps me at the same page I was at.

It does work. :) Make sure you are doing:

window.location.href = 'protectedpage.html';

and not

window.location.href ('protectedpage.html');

Using your code as below...

:( Still doesn't work. The page I was on just kinda flashes and stays the same. The only thing that changes is that I've got "?x=65&y=27" appended to the end of my URL afterwards.

I understand that it should work...it just doesn't, and I don't know where I'm messing up. Does it matter that it's all in the header? :(
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
document.location.href, not window.location.href

If you're using Mozilla, Firefox, or SeaMonkey, keep the JavaScript console open (Tools->Web Development->JavaScript Console) - it tells you about script problems. You can also enable "strict mode" for even more reporting.
 

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
Originally posted by: CTho9305
document.location.href, not window.location.href

If you're using Mozilla, Firefox, or SeaMonkey, keep the JavaScript console open (Tools->Web Development->JavaScript Console) - it tells you about script problems. You can also enable "strict mode" for even more reporting.


document.location.href doesn't work either :(

You guys may think I'm crazy by now, but you can check for yourself here:
http://www.psalms4610.com/Untitled-1.html

Viewing the source will show you the script.

password is 'password'
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
This is weeeeird. I stepped through it with the JavaScript Debugger, and it works right, but when I run it normally it doesn't.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Why do you have a password() and a passWord()?

edit: seems to work now. Did you change something? Note that this isn't a good way to protect pages - I can easily see that your other password is "coonrod" and the protected page is "cgwedding.html".
 

Stangs55

Golden Member
Oct 17, 2004
1,130
0
0
Originally posted by: CTho9305
Why do you have a password() and a passWord()?

edit: seems to work now. Did you change something? Note that this isn't a good way to protect pages - I can easily see that your other password is "coonrod" and the protected page is "cgwedding.html".

Seems to work now? umm...It's still not working for me. Any new ideas?

And, lol, of course you can see the password from the main page :) , this isn't really designed for super-secret security...just mainly for me to learn for the future when I will need to password protect pages.

Also, if anyone can point me to some easly sample SQL/php code so that I can store usernames/passwords properly, that would be great! :) I'm already build the db actually, I really just need to the code snippet to figure out how to prompt for a password, check if it's in the table, and then link accordingly.