1 Replies - 1707 Views - Last Post: 18 January 2009 - 11:19 AM

#1 pandaduc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-December 08

Problem of Javascript in HTML 4.01 transitional

Posted 17 January 2009 - 08:49 PM

I got the problem about using javascript in HTML 4.01 transitional.

When I use javascript no HTML 4.01 transitional , my validation form works , but when I put " <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">" , my validation form doesn't work in Firefox , but It works in IE and google chrome

how can I fix it ? Thank you for your help .

<html>
<head>
<title>Login form</title>
<script type="text/javascript">
function validate(){

var un=info.username.value;
var pw=info.password.value;
var len=un.length;
var len2=pw.length;

if(len==0 && len2==0){
alert("Your Username and Password are still empty");
return false;

}

if(len==0){
alert("Your Username is still empty");
return false;
}
if(len2==0){
alert("Your Password is still empty");
return false;
}
return true;

}

</script>
</head>

<body>
<form name="info" action="server.php" method="get" onsubmit="return validate()">
<table border='0' align=center>
<tr>
	<td>User Name: </td>
	<td><input type="text"name="username" ></td>
</tr>
<tr>
	<td>Password:</td>
	<td><input type="password" name="password"></td>
</tr>
<tr align=center>
	<td colspan='2'><input type="submit" value="Submit"></td>
</tr>

</table>
</form>
</body>
</html>



This post has been edited by pandaduc: 17 January 2009 - 09:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem of Javascript in HTML 4.01 transitional

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Problem of Javascript in HTML 4.01 transitional

Posted 18 January 2009 - 11:19 AM

The doctype is going to hold your feet to the fire a little and make you use proper coding here. So the problem is with these two lines...

// Wrong
var un = info.username.value;
var pw = info.password.value;

// Right...
var un = document.info.username.value;
var pw = document.info.password.value;



Notice how we use the "document" to specify this current page and then the form. The reason it works in IE is because it often assumes document. In either case you should always use "document" when referring to elements on a page. It is good programming practice and more in line with doctype standards.

So once you change the two lines everything should be working fine again.

Enjoy!

"At DIC we be document handling code ninjas... watergate? Yeah we helped with the documents. Nixon said we would get girls and naturally being geeks we were up for that." :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1