javascript - Open a URL always in the same new tab - Stack Overflow

How can I use window.open to open a link in a new tab?Repeated calling should reload that same tab and

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 a URL. Repeated click should reload in the same tab/window.

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 a URL. Repeated click should reload in the same tab/window.

Share Improve this question edited Sep 29, 2023 at 7:58 bluish 27.4k28 gold badges125 silver badges184 bronze badges asked May 17, 2016 at 3:17 user5858user5858 1,2217 gold badges42 silver badges84 bronze badges 1
  • stackoverflow./questions/7077770/… – Ronnie Smith Commented May 17, 2016 at 3:19
Add a ment  | 

5 Answers 5

Reset to default 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.

You can try this

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

JSFiddle : https://jsfiddle/p26c2atz/

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.";
      form.target = "newWin";
      document.body.appendChild(form);
      form.submit();
  }

</script>

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

Other way will be:

<form>
  <input type="button" id="openWindow" value="Open Window" onclick="window.open('http://www.example.','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.

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>

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 the same!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263182a4619306.html

相关推荐

  • javascript - Open a URL always in the same new tab - Stack Overflow

    How can I use window.open to open a link in a new tab?Repeated calling should reload that same tab and

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信