Search code examples
javascriptjquerywordpresscookiesbrowser-history

Is it possible to create a button that opens a specified website in a new tab and closes the current one?


I am creating a WordPress website for a client. This website offers support for victims of abuse, and my client wishes for a "panic" button on the website that, when clicked, either:

a) sends the user to a specified website (for example google.com) and then erases the history of the Back button

b) opens a specified website in a new tab (which eliminates the Back button issue) and then closes the current one.

I tried adding a simple button like this:

<button onclick="window.close();">Close</button>

But then I get this in the console:

[Warning] Can't close the window since it was not opened by JavaScript

If this is not possible to do by using Javascript, are there any other ways I can go about doing it?


Solution

  • Not possible.

    The problem you're up against is that the browser doesn't allow Javascript to control things like opening and closing tabs, and rightly so. A web page really shouldn't be able to access or control things outside of its tab, this is a concept called sandboxing. Web pages are free to play in their own sandbox, but aren't allowed to know about or change things outside their sandbox. "Browser history" and "Other tabs" are both outside your web page's sandbox.

    It's not that this is impossible in JavaScript, it's going to be impossible for any well designed browser, full stop. It's not a question of what technology you could use to accomplish this, it's a behavior that's explicitly prohibited by design. If you could do this on a major browser, you would probably be offered a bug bounty. Imagine what a malicious actor could do with the ability to modify the history in the back button, or to close other tabs.

    Beyond that, I'm concerned about the approach that your employer is taking. Abusers are often more technically savvy than their victims, and a "panic button" like that won't even affect the browser's overall history, let alone defeat any advanced monitoring like spyware, or a fancy router's DNS monitoring. Victims of abuse should be discouraged from using devices or networks they believe are compromised and should seek help through trusted and secure channels of communication. I'm troubled that any sort of "panic button" functionality could create a false sense of security for a user that is not security-savvy.