How to close the current browser window/tab in React?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
daweimaucommented, Nov 10, 2020

https://mmvcr.csb.app/

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.

3reactions
vincent714commented, Oct 16, 2020

hi @Tevinthuku don’t know how u tried, just try to go to https://mmvcr.csb.app/ directly by copying it to the browser, i am here attaching the warning received.

image

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found