简体   繁体   中英

how to close page in Chrome and MS Edge using javascript in first time?

I have two pages which are naming FATHER & CHILD PAGE.

FATHER PAGE opens CHILD PAGE using Javascript code:window.open().

Now, I would like to close FATHER PAGE from CHILD PAGE using window.opener.close().

In first time to open Chrome or MS Edge browser, CHILD PAGE CANNOT close FATHER PAGE using window.opener.close(), but it works when exists several pages in the same time and same browser instance.

How to close FATHER PAGE from CHILD PAGE when browser is opened in first time without other pages in the browser?

FATHER PAGE: window.open('CHILD PAGE','_blank');

CHILD PAGE: window.opener.close();

I agree with diegod's comment. If you open F12 devtools console tab, you'll see this warning:

Scripts may close only the windows that were opened by them.

That's the same meaning with the MDN doc . You can't close the first parent window because it's not opened by window.open() script. The limitation of window.close() is for security and user-experience. For more detailed information, you can also refer to this blog .

So the conclusion is you just can't do it like that.

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