Google Sites, HELP! |
Google Sites, HELP! |
jainilsutaria |
Dec 4 2014, 06:37 PM
Post
#1
|
Group: Members Posts: 2 Joined: 4-December 14 Member No.: 21,890 |
So I wrote the code below for the site https://sites.google.com/pilabshare and I want code such that when the user signs in with passwords: ["best", 'stupid', 'hprox', 'short', 'lnmi'], and with usernames ["jmoney", "ksut", "ish16", "neggyg", "lphp"] (respectively), it redirects the user to youtube.com (as a test site) Why isnt this working? CODE <!DOCTYPE html> <html> <body> Username: <input type="text" id="user" value="USERNAME"> Password: <input type="text" id="pass" value="PASSWORD"> <p>Please enter your username and password</p> <button onclick="OPENPAGE()">Sign In</button> <p id="demo"></p> <script> var passlist = ["best", 'stupid', 'hprox', 'short', 'lnmi'] var userlist = ["jmoney", "ksut", "ish16", "neggyg", "lphp"] function OPENPAGE() { var usern = document.getElementById("user").value; var passn = document.getElementById("pass").value; var testnum = 0 for (var item in passlist) { if ((passlist[item] == passn) && (usern == userlist[item])) { window.open("http://www.youtube.com","_self") } } } </script> </body> </html> |
CharlesEF |
Dec 5 2014, 12:27 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Besides your small syntax errors (missing ; at end of lines) you have 1 major problem. Your for loop only loops thru the passlist and unless the userlist contains the matching name in the same position as the passlist your window.open code will never fire. If you want to compare any combination then you should use a 2nd loop, like:
CODE <!DOCTYPE html> <html> <body> Username: <input type="text" id="user" value="USERNAME"> Password: <input type="text" id="pass" value="PASSWORD"> <p>Please enter your username and password</p> <button onclick="OPENPAGE()">Sign In</button> <p id="demo"></p> <script> var passlist = ["best", 'stupid', 'hprox', 'short', 'lnmi']; var userlist = ["jmoney", "ksut", "ish16", "neggyg", "lphp"]; function OPENPAGE() { var usern = document.getElementById("user").value; var passn = document.getElementById("pass").value; for(var user in userlist) { for(var pass in passlist) { if((passlist[pass] == passn) && (usern == userlist[user])) { window.open("http://www.youtube.com","_self"); } } } } </script> </body> </html> |
Christian J |
Dec 5 2014, 02:51 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,673 Joined: 10-August 06 Member No.: 7 |
First, note that javascript password protection is not reliable at all: http://htmlhelp.com/faq/html/publish.html#password
unless the userlist contains the matching name in the same position as the passlist Isn't that a good thing? Otherwise any of the usernames would match any of the passwords. To make the order of usernames and corresponding passwords more intuitive(?) you might use a two-dimensional array instead of two independent ones: CODE var user_pw = [ ['jmoney', 'best'], ['ksut', 'stupid'], ['ish16', 'hprox'] ]; function OPENPAGE() { var usern = document.getElementById("user").value; var passn = document.getElementById("pass").value; for(var i in user_pw) { if ((usern == user_pw[i][0]) && (passn == user_pw[i][1])) { // redirect } } } |
CharlesEF |
Dec 5 2014, 03:03 AM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
It depends on the fact if that is what the OP wanted. Maybe it is but I couldn't tell by the information provided. I was going to wait for the OP to reply to correct me, if I was wrong. If you are correct then your code is better suited for the OP's purpose.
|
jainilsutaria |
Dec 6 2014, 02:55 PM
Post
#5
|
Group: Members Posts: 2 Joined: 4-December 14 Member No.: 21,890 |
Besides your small syntax errors (missing ; at end of lines) you have 1 major problem. Your for loop only loops thru the passlist and unless the userlist contains the matching name in the same position as the passlist your window.open code will never fire. If you want to compare any combination then you should use a 2nd loop, like: CODE <!DOCTYPE html> <html> <body> Username: <input type="text" id="user" value="USERNAME"> Password: <input type="text" id="pass" value="PASSWORD"> <p>Please enter your username and password</p> <button onclick="OPENPAGE()">Sign In</button> <p id="demo"></p> <script> var passlist = ["best", 'stupid', 'hprox', 'short', 'lnmi']; var userlist = ["jmoney", "ksut", "ish16", "neggyg", "lphp"]; function OPENPAGE() { var usern = document.getElementById("user").value; var passn = document.getElementById("pass").value; for(var user in userlist) { for(var pass in passlist) { if((passlist[pass] == passn) && (usern == userlist[user])) { window.open("http://www.youtube.com","_self"); } } } } </script> </body> </html> I used this but it still doesn't work. Any other reason why? Is it because of the redirect code? |
CharlesEF |
Dec 6 2014, 04:17 PM
Post
#6
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Well, I tested the code and it works fine for me. If any user name in the userlist matches any password in the passlist then I'm taken to youtube.com
|
Christian J |
Dec 6 2014, 04:19 PM
Post
#7
|
. Group: WDG Moderators Posts: 9,673 Joined: 10-August 06 Member No.: 7 |
Which browser (and version) do you use? Do you get any script error? Did you enter correct username and password?
|
Lo-Fi Version | Time is now: 25th May 2024 - 09:03 PM |