How to close the current browser window/tab in React?
See original GitHub issueI want to close the current tab or window when the user clicks “Close” button.
onClose() {
window.opener = null;
window.open('', '_self');
window.close();
}
I tried the above code, but it’s not working. My env is:
Windows 10 x64
Chrome 79.0.3945.88
React 16.12.0
Is it supported?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Close current browser tab on button click, using reactjs
by open an empty page in the same page then close it. You could try that window.open("about:blank", "_self"); ...
Read more >HTML : Close current browser tab on button click, using reactjs
HTML : Close current browser tab on button click, using reactjs [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] HTML : Close...
Read more >Handle the Browser Tab close event in React | bobbyhadz
To handle the browser tab close even in React, use the `useEffect` hook to add an event listener. Listen for the `beforeunload` event....
Read more >[Solved]-How do I close the current browser tab on ReactJS?
How do I close the current browser tab on ReactJS? · Close current browser tab on button click, using reactjs · ReactJS Show...
Read more >Close current browser tab on button click, using reactjs
Is there a way I can close the current browser tab with a press on the button, using reactjs? I've tried window.close() and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This script only works if the user has opened the page by ctrl+clicking into a new tab. If you copy paste the link into navbar, then it doesn’t work.
It’s because this behaviour (closing a window that the user opened, via script) generally isn’t allowed by modern browsers , although there seemingly still edge cases and workaround attempt, with varying success, as seen here.
Hi @vidukapoor I found that you can’t go to the web directly by copying it to the browser, just click the link provided here. https://mmvcr.csb.app/
it will work.