[英]How can i close an already opened window and then open a new window every time i click a button in angular?
I want to open a window using window.open().我想使用 window.open() 打开一个窗口。 but i have to make sure that if that same window has already opened previously , i have to first close that window then i have to open a new one .但我必须确保,如果同一个窗口之前已经打开,我必须先关闭那个窗口,然后我必须打开一个新窗口。
try this code试试这个代码
<button onclick="openNewWindow()">Open New Window</button>
var popup_window;
function openNewWindow(){
if(popup_window != undefined){
popup_window.close();
}
popup_window = window.open("");
}
Try following code试试下面的代码
let openedWindow;
let url = "www.example.com"
function openWindow() {
if(openedWindow) {
openedWindow.close();
}
openedWindow = window.open(url);
}
NOTE : The Window.close()
method can only be called on windows that were opened by a script using the Window.open()
method.注意: Window.close()
方法只能在使用Window.open()
方法由脚本打开的窗口上调用。 If the window was not opened by a script, an error similar to this one appears in the console:如果该窗口不是由脚本打开的,控制台中会出现与此类似的错误:
Scripts may not close windows that were not opened by script.脚本可能不会关闭不是由脚本打开的窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.