Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is javascript function called even before onClick?

 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following simple code.It is showing no object error a common in Javascipt But the Fn that I intend to be called after onCLick is invoked directly when I run the servlet.The servlet just contains Login and Password fields.I wished to have both SUBMIT and CANCEL buttons in the same row.So instead of submitting form by using input type=SUBMIT I m using input type=BUTTON and submitting the form by onClick event.
One more Q.
Can I call two events separated by commas by clicking the same button once?
HERE IS THE CODE ------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Administrator extends HttpServlet
{
public void init(ServletConfig config)throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<meta http-equiv=Content-Type"+
" "+"content=text/html; charset=iso-8859-1>");
out.println("<meta name=GENERATOR content=Microsoft FrontPage Express 2.0>");
out.println("<title>AdministratorLogin.html</title>");
out.println("<script language=javascript>");
out.println("<!--");
out.println("function checkforblanks(){if(document.myform.loginname.value==''){alert('Please'+' '+'enter Login Name.');return'+' '+'false;}else{if(document.myform.password.value==''){alert('Please enter password.');return'+' '+'false;}}return true;}");
out.println("-->");
out.println("</script>");
out.println("</head>");
out.println("<body bgcolor=#00FFFF>");
out.println("<p align=center><font color=#0000FF"+ " "+"size=6><strong>Administrator"+
" "+"Login </strong></font></p>");
out.println("<p> </p>");

out.println(" <div align=center><center>");
out.println(" <table border=0 width=75%>");
out.println(" <form name=myform method=post>");
out.println(" <tr>");
out.println(" <td><strong>Login Name</strong><font"+
" "+"color=#FF0080><strong>*</strong></font></td>");
out.println(" <td><strong>:</strong></td>");
out.println(" <td><input type=text size=20 maxlength=20"+
" "+"name=loginname></td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=33%><strong>Password</strong><font"+
" "+"color=#FF0080><strong>*</strong></font></td>");
out.println(" <td width=33%><strong>:</strong></td>");
out.println(" <td width=34%><input type=password size=20"+
" "+"maxlength=20 name=password></td>");
out.println(" </tr>");
out.println(" </table>");
out.println("</center></div>");
out.println("<p align=center> </p>");
out.println("<div align=center><center>");
out.println("<table border=0>");
out.println(" <tr>");
out.println("<td><input type=button value=Submit onClick=return"+" "+
"checkforblanks()></td>");
out.println("<td><input type=reset value=Cancel></td>");
out.println(" </tr>");
out.println("</form>");
out.println("</table>");
out.println("</center>");
out.println("</div>");
out.println("</body>");
out.println("</html>");
}
}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this thread to the Servlets and JSP's forum because I think you will get a better response over there.
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.println("<td><input type=button value=Submit onClick=return"+" "+
"checkforblanks()></td>");
did you try:
out.println("<td><input type=button value=Submit onClick='return"+" "+
"checkforblanks()'></td>");
instead? I put 'return checkforblanks()' in a single quotes.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.println("function checkforblanks(){
if(document.myform.loginname.value==''){
alert('Please'+' '+'enter Login Name.');
return;
}else if(document.myform.password.value==''){
alert('Please enter password.');return;
}//code reaches here means fields are not empty so
document.myform.submit();
");
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything wrong in your code except what Yuriy and Kumar pointed out here. But what i'm wondering is that even with those errors you might have got messages "error in page" or no call to your javascript function but not "no object found". I got an error like these sometime ago and it worked for me when i placed my javascript after the html </FORM> statement(so as to make sure the "document.myform.loginname.value" reference is made after the actual form declaration). Try that ,may be it helps

Ajan
[This message has been edited by Ajan Balakrishnan (edited February 04, 2001).]
 
jos xavier
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Vaibhav,
Can you try using the onSubmit in the form tag.?
<form name="xx" method="post" action="xxx" obSubmit="return checkforblanks(document.xx)">{
and in the javascript call mention
funtion checkforblanks(FormObj){
}
try this out and check the results...
bye
Joseph
 
vaibhav punekar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys I ll try it.I think joseph's code seems interesting .Generally either we use onClick for buttons.I ll try using it in Form tag.but anyhow my code seems to be working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic