最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

java - what is the reason for Window.location.href not working sometimes? - Stack Overflow

matteradmin4PV0评论

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);

%>

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);

%>
Share Improve this question asked Oct 24, 2013 at 8:01 Jagathesewaren KuppurajJagathesewaren Kuppuraj 911 gold badge3 silver badges11 bronze badges 1
  • 4 You are submitting form and redirecting to another form same time. It's not right – karaxuna Commented Oct 24, 2013 at 8:04
Add a ment  | 

6 Answers 6

Reset to default 2

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()">

Try this:

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

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

<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.

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

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

actionResponse.sendRedirect("CallService.jsp");

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);" > 
Post a comment

comment list (0)

  1. No comments so far