what is the reason for Window.location.href not working sometimes?

jsp page for submit button onclick function in javascript. when i click button first time not redirected to CallService.jsp. I click the button two to three times after that only that page will be redirected.

Pls give me the solution or reason

My Code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<script type="text/javascript">

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
}


</script>


<body>

<h3>User Login</h3> <br>

<form name="userlogin" method="post">

User Name : <input type="text" name="uname" id="uname"> <br>

Password : <input type="text" name="pwd" id="pwd"> <br>

<input type="submit" value="Submit" onclick="sample(this);" > 
<input type="reset" value="Cancel"> 

<div id="name"></div>
<div id="email"></div>

</form>

</body>
</html>

CallService.jsp

<%


  String uname = request.getParameter("username");
  //String pwd = request.getParameter("password");


  out.println(uname);

%>

Upvotes: 0

Views: 4725

Answers (6)

StaleMartyr
StaleMartyr

Reputation: 778

Since you are fetching the data using javascript here :

var uname = document.getElementById("uname").value;

theres no need to use a 'submit' button. Try to change

<input type="submit" value="Submit" onclick="sample(this);" > 

into

<input type="button" value="Submit" onclick="sample(this);" > 

Upvotes: 0

Bharath R
Bharath R

Reputation: 1531

Why dont you try redirecting from you java class, like this

actionResponse.sendRedirect("CallService.jsp");

Upvotes: 0

Adam Moszczyński
Adam Moszczyński

Reputation: 3556

please try:

function sample()
{

    var uname = document.getElementById("uname").value;
    //var pwd = document.getElementById("pwd").value;

    window.location.href("CallService.jsp?username="+uname);
    //FUNCTION MUST RETURN FALSE!!
    return false;
}

<input type="submit" value="Submit" onclick="return sample(this);" > 

this way js function will need to be evaluted before making postback

Upvotes: 0

Raghav
Raghav

Reputation: 7513

<input type="submit" value="Submit" onclick="sample(this);" > 

You cannot submit and redirect this.

In your servlet / jsp where the form is submitted, redirect the page.

Upvotes: 1

karaxuna
karaxuna

Reputation: 26940

Try this:

window.location.href = "CallService.jsp?username="+uname;

You are submitting form and redirecting to another form same time. It's not right

Upvotes: 2

Manwal
Manwal

Reputation: 23826

Your button type is

<input type="submit" value="Submit" onclick="sample(this);" > 

replace it with:

<input type="button" value="Submit" onclick="sample(this);" > 

or

Call your function when submitting form like this:

<form method="post" name="fromname" onsubmit="yourfunction()">

Upvotes: 2

Related Questions