admin管理员组文章数量:1417457
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
5 Answers
Reset to default 4You 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!
本文标签: javascriptOpen a URL always in the same new tabStack Overflow
版权声明:本文标题:javascript - Open a URL always in the same new tab - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745263182a2650451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论