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!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745263182a4619306.html
评论列表(0条)