The JavaScript code below is executed every time the user selects the login button on my webpage, the function basically ensures that the username and password fields have received text (and then strips away any leading or trailing white space) and then redirects the browser to the php login script. The code works perfectly in IE, Firefox and Opera yet it doesn’t in Safari and Chrome.
In Safari and Chrome, the textbox values are undefined. Other browsers return the values typed perfectly.
var button;
var name;
var pass;
function logIn()
{
name.value = name.value.replace(/^\s+|\s+$/g,"");
pass.value = pass.value.replace(/^\s+|\s+$/g,"");
if(name.value == "")
{
alert("Please Enter A Username");
}
else if(pass.value == "")
{
alert("Please Enter A Password");
}
else
{
window.location.href = "loginScript.php?username=" + name.value + "&password=" + pass.value;
}
}
function onLoad()
{
button = document.getElementById('button');
name = document.getElementById('username');
pass = document.getElementById('password');
button.onclick = logIn;
}
window.onload = onLoad;
I believe the variable you’re using, name, is a reserved keyword in Chrome.
Try changing the name variable to something like username.
EDIT: Changed pass to password as well