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 self.close()
but none of the JavaScript methods seem to work.
I don't want the event of close window in React. I want to close the browser tab itself. window.close()
can be used only when window.open()
is used.
open("https://xyz.abc/", "_self", ""); console. log("open window"); }, []); Then make function of close and add onclick to close() which has window. close();
A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.
To close a window or tab that was opened using JavaScript, call window. close() .
You need to use some state to represent the currently selected tab. Then you can set this state from either navs or your external component. Add this inside your component function: const [currentTab, setCurrentTab] = useState("first"); const handleSelect = (eventKey) => { setCurrentTab("second"); };
It's not allowed by the browser to close not self open tab by window.open()
so it's kind of work around and convincing the browser it's open by you
by open an empty page in the same page then close it.
You could try that
window.open("about:blank", "_self");
window.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With