Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 589
John Avatar asked Mar 05 '19 06:03

John


People also ask

How do I close the current tab in React?

open("https://xyz.abc/", "_self", ""); console. log("open window"); }, []); Then make function of close and add onclick to close() which has window. close();

How detect browser or tab close in React JS?

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.

How do I close the current tab in JavaScript?

To close a window or tab that was opened using JavaScript, call window. close() .

How do I change the active tab in React JS?

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"); };


1 Answers

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();
like image 176
mooga Avatar answered Oct 22 '22 21:10

mooga