1

I'm unable to find the answer.

How can I use window.open to open a link in a new tab?

Repeated calling should reload that same tab and not open a new one.

I've a button when clicked should load an url. Repeated click should reload in the same tab/window.

user5858
  • 1,082
  • 4
  • 39
  • 79

5 Answers5

4

You need to pass a name as the second parameter to window.open

As long as the tab with that name has not been closed, it will be reused.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

You can try this

window.open('http://www.example.com','mywindow');

JSFiddle : https://jsfiddle.net/p26c2atz/

Munawir
  • 3,346
  • 9
  • 33
  • 51
0

In plain JS

<form>
  <input type="button" id="openWindow" value="Open Window" onclick="newTab()">
</form>
<script>
  function newTab() {
      var form = document.createElement("form");
      form.method = "GET";
      form.action = "http://www.example.com";
      form.target = "newWin";
      document.body.appendChild(form);
      form.submit();
  }

</script>

You can see it in action in https://jsfiddle.net/jprbj21u/

Other way will be:

<form>
  <input type="button" id="openWindow" value="Open Window" onclick="window.open('http://www.example.com','newWin')">
</form>

The link will open in a tab named "newWin". As long as you open the same window any new URL will load on it.

Roldan
  • 225
  • 2
  • 14
0

Here you can do change target after focusout. I tested and its work.

Link

<script type="text/javascript">
$('a').focusout(function(e) {
    $(this).attr('target','_self');
});
</script>
Kirti
  • 1
  • 1
0

window.open(url,'someconstant',"width=600,height=700")

Here on click-event, there will be a new window opened with the instance named 'someconstant', when you do a click again, the window will be opened again overwriting the previous window as your instance is same!.

Hope this helped.! Happy Coding!