简体   繁体   中英

Close window from browser history

I am working on a web application where I am using JavaScript for the client side scripting. Now my requirement is to close all the opened window which were opened through window.showModalDialog() .

For this, I read the history of the browser using window.history.length , but I do not know how to close each window. This works well for window.open() , but not for window.showModalDialog() .

Could you please guide me to move forward?

You can close the opened window as follows:

To Open:

var window1=window.open("http://somedomain.com");
var window2=window.open("http://someotherdomain.com");

To Close

window1.close();

window2.close();

But be sure you call window1.close() and so on.. on the same script where you opened it.

You should not be using window.showModalDialog . Firefox has deprecated it , and Chrome has removed it . Also take a look at window.showModalDialog: What It is and Why You Should Never Use It .

The idea of showModalDialog is that all scripts are paused while the modal window is open. Thus, using window.close doesn't work, since as long as the window is open, no more scripts are being executed.

One possibility is to have JavaScript in the modal dialog so that is closes itself . You will not be able to close it from outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM