1

New tab works good, but for some reasons current page haven't redirect (window.location from changeLocation() doesn't work)

function changeLocation() call success everytime when I click button, but location doesn't change.

Basically it seems like window.location doesn't work because of form submit (although I use target="_blank"). But if I add return false after window.location to function, I'm not able to submit form after changing location (because I'm already on another page) and also submit before changing location impossible.

Is this possible to make it works somehow? Thanks.

function changeLocation (){
  window.location = "http://bing.com";
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>EXAMPLE</title>
</head>
<body>
<form action="https://google.com" target="_blank">
  <input type="text">
  <button onclick="changeLocation()">Submit</button>
</form>
</body>
</html>
Alexander Seredenko
  • 799
  • 3
  • 9
  • 26

3 Answers3

2

This code works for firefox

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>EXAMPLE</title>
  <script type="text/javascript">
      function changeLocation (){
  window.location = "http://bing.com";
      }
  </script>
</head>
<body>
<form action="https://google.com" target="_blank">
  <input type="text">
  <button onclick="changeLocation()">Submit</button>
</form>
</body>
</html>

google.com is being loaded in the new tab and the current tab us being loaded with bing.com

skid
  • 958
  • 4
  • 13
  • 29
1

To solve it, you need to add setTimeout to function like this.

function changeLocation (){
 setTimeout(function(){
  window.location = "http://bing.com";
 },200);
}

No ideas why, but it works perfect.

Alexander Seredenko
  • 799
  • 3
  • 9
  • 26
0
Dim returnUrl = Request.UrlReferrer.ToString()
Response.Write("<script> setTimeout(function(){window.location = """ + returnUrl + """;},200);window.open (""" + loginurl + """,'_blank');</script>")
Musab
  • 1,067
  • 12
  • 12