Frank
Frank

Reputation: 31086

Why javascript auto submit doesn't work in my jsp page?

I have a Java web app, which has a jsp login page, I added a javascript to make it auto login after 3 seconds, but it's not working, the code looks like this :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" href="styles/loginForm.css" type="text/css"/>
    <link rel="stylesheet" href="styles/body.css" type="text/css"/>
    <script type="text/javascript" src="javascript/login.js"></script>

    <c:choose> 
        <c:when test="${browserType.msie}">
            <link rel="stylesheet" href="styles/header_ms.css" type="text/css"/>
        </c:when>
        <c:otherwise>
            <link rel="stylesheet" href="styles/header.css" type="text/css"/>
        </c:otherwise>    
    </c:choose>

    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
    <title>Support Logon</title>
    <script language="javascript" type="text/javascript">
        function initialize()
        {
          alert("Hello = 1");
          setTimeout('submitForm()',3000); //delay 3 seconds
          alert("Hello = 10");
        }
        function submitForm()
        {
          alert("Hello = 2");
          // window.document.loginForm.submit();
          document.forms["login_form"].submit();
          alert("Hello = 3");
        }
    </script>
  </head>

<body onload="initialize()">
<%@ include file="header.jsp" %>
<div id="login_body">
    <form method="post" action="login.html" name="loginForm">
        <div id="login_wrapper">

            <div id="login_form">
                <div id="login_title">Please Login</div>
                <div id="fail_message">${failedLoginMessage}</div>
                    <div id="user_name">
                        <div id="name_label">Username:</div>
                        <div id="name_text"><input name="j_username" size="22" id="textbox" value="${commandBean.j_username}"/></div>
                    </div>

                    <div id="password">
                        <div id="pw_label">Password:</div>
                        <div id="pw_text"><input type="password" name="j_password"  size="22" id="textbox" value="${commandBean.j_password}"/></div>
                    </div>

                    <div id="remember_me">
                        <c:choose> 
                            <c:when test="${commandBean.fail}">
                                <div id="remember_box"><input name="rememberMe" id="checkbox_style" type="checkbox"/></div>
                                <div id="remember_label">Remember Me</div>
                            </c:when>
                            <c:otherwise>
                                <div id="remember_box"><input checked="checked" name="rememberMe" id="checkbox_style" type="checkbox"/></div>
                                <div id="remember_label">Remember Me</div>
                            </c:otherwise>    
                        </c:choose>  
                    </div>

                    <div id="submit">
                        <div id="submit_button"><input name="submit" value="Login" id="button_style" type="submit"/></div>
                        <div id="clear_button"> <input name="clear"  value="Clear" id="button_style" type="button" onclick="clearLoginForm();"/></div>
                    </div>

I tried all the following, none of them worked :

document.forms["loginForm"].submit();
document.forms["login_form"].submit();
window.document.loginForm.submit();
window.document.login_form.submit();

All I got was :

Hello = 1
Hello = 10
[ delay 3 sec. ]
Hello = 2

In that order, why ? How to auto submit ?

Upvotes: 0

Views: 3850

Answers (2)

coder
coder

Reputation: 4466

The name of your input button is submit, so when you are calling form.submit() its getting confused with your submit button. Change the name of your submit button in following line

 <div id="submit_button"><input name="submit" value="Login" id="button_style" type="submit"/></div>

and thn following should work

document.forms["loginForm"].submit();

You can find working example of your code here http://jsfiddle.net/Lg7hkepo/

Upvotes: 2

desert
desert

Reputation: 29

Did you see any script error?

The simplest way to fix,

  1. Put a button to call "submitForm()", click the button to test if that function work, you can also try, give the form id, then change to

document.getElementById("formid").submit(); //use your form id;

  1. Till the button works, you can switch back to auto call the function;

Upvotes: 1

Related Questions