File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Why is  javascript function called even before onClick? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why is  javascript function called even before onClick?" Watch "Why is  javascript function called even before onClick?" New topic

Why is javascript function called even before onClick?

vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
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 javax.servlet.*;
import javax.servlet.http.*;
public class Administrator extends HttpServlet
public void init(ServletConfig config)throws ServletException
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
PrintWriter out = res.getWriter();
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("<script language=javascript>");
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("<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("<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"+" "+
out.println("<td><input type=reset value=Cancel></td>");
out.println(" </tr>");

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 02, 2001
Posts: 413
out.println("<td><input type=button value=Submit onClick=return"+" "+
did you try:
out.println("<td><input type=button value=Submit onClick='return"+" "+
instead? I put 'return checkforblanks()' in a single quotes.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
out.println("function checkforblanks(){
alert('Please'+' '+'enter Login Name.');
}else if(document.myform.password.value==''){
alert('Please enter password.');return;
}//code reaches here means fields are not empty so
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
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

[This message has been edited by Ajan Balakrishnan (edited February 04, 2001).]
jos xavier

Joined: Feb 02, 2001
Posts: 14
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...
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
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.
I agree. Here's the link:
subject: Why is javascript function called even before onClick?
jQuery in Action, 3rd edition