Solution to the problem of invalid jump setting window. location. href in javascript

  • 2021-07-18 06:56:33
  • OfStack

Solution to the problem of invalid jump setting window. location. href in javascript

Problem situation

Invalid window. location. href jump set in JS

The code is as follows:


<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" > Confirm a reservation </a> 
      </div> 

The reason is that the href jump transfer of the a tab is performed before the jump set by window. location. href:

If the form is form, form submission will be performed first.

It is no longer on the current page after submission. So window. location. href is invalid.

Solution 1

Add to the js function


window.event.returnValue=false

This property is placed in the onclick event in the Submit Form where the form will not be submitted this time, and if placed in the Hyperlink, the Hyperlink href property is not executed this time.

window. location. href jumped successfully after changing to the following code:


<script type="text/javascript"> 
  function checkUser() 
{  
   if(2!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ;  
   window.event.returnValue=false; 
   } 
} 
 </script>  
 
<div class="extra"> 
     <a class="ui blue right floated primary button" onclick="checkUser()" href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime }" rel="external nofollow" rel="external nofollow" > Confirm a reservation </a> 
      </div> 

Solution 2

In the click event, onclick= "checkUser ()" becomes onclick= "return checkUser (); "

And return false in checkUser; In this case, href of a tag will not be executed. In this way, window. location. href can jump smoothly.

The code is as follows:


<script type="text/javascript"> 
  
  function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
} 
 </script> 
 
 <div class="extra"> 
     <a class="ui blue right floated primary button" onclick="return checkUser();"  
 
href="bookConfirm?userId=${account.id}&roomNum=${room.roomNum}&stime=${stime }&etime=${etime  
 
}"> Confirm a reservation </a> 
      </div> 

Solution 3

If it is an form body commit, you can also change summit to button to call js commit, so that window. location. href will also perform a successful jump before

commits summit.

As follows:


function checkUser() 
{  
   if(<%=flag%>!=1){ 
    window.location.href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ; 
   return false; 
   } 
 document.getElementById("form").submit(); 
} 
 
 
  <form action="addRoom" method="post"  name="from" id="form"> 
      <table align="center" border="1" class="commTable"> 
        <tr> 
          <td class="right"><span 
            style="font-weight: blod;"> Room number: </span></td> 
          <td><input type="text" name="roomNum" size="25" 
            id="roomNum" /></td> 
        </tr> 
        <tr> 
          <td colspan="2" align="center"><button  value=" Add " 
            onclick="checkUser()" /></td> 
        </tr> 
      </table> 
    </form> 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: