Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

i'm looking for a conclusive, 100%-effective way to block this latest hideous trend of marketers and scammers alike. you'll be familiar with it yourself - it's a very easy way to tell whether or not to trust a website if it flags up a warning like this when you try and navigate away:

"are you sure?" spam

i'm aware some sites use this benevolently - even superuser does it when attempting to close the browser - but i hate it. if i close a tab it's because i want it closed. i do NOT appreciate my browser second-guessing my choices. if there is a way to kill this, i expect it to kill superuser's implementation of it as well.

there are more than a few userscripts that aim to kill this, and i'll post them here. all of them refuse to work with a certain link, which i'll also post, which renders them moot immediately.

here's the site that somehow manages to overcome any protection against it:

(PLEASE NOTE: this is a dangerous site! do NOT visit it if you are not protected with an anti-virus, if you're running an obsolete web browser, if you have java installed, etc., etc. it WILL attempt to mislead you. also, be aware it has a penchant for playing awful music immediately upon loading at full volume so best to put the mute on.)

the above site will either load something about free sex videos or, more often, a video sharing site trying to be youtube. this evades any "block onunload" script i throw at it. you can also try posting a reply to this topic (i tried commenting, but that didn't cause the dialogue, so try answering, which i can't yet) and then closing your browser - superuser do the same thing, for their sins.

thanks for any suggestions. let's kill this user-hostile nonsense for good.

share|improve this question

marked as duplicate by Tog, Heptite, Kevin Panko, Dave, Shekhar Feb 20 '14 at 17:33

This question was marked as an exact duplicate of an existing question.

You could whitelist javascript for only those sites you want it to run on, which would essentially solve this problem. – Paul Feb 15 '14 at 22:07
whitelisting javascript strikes me as an incredibly overreaching solution to tackle one annoyance. i shouldn't have to alter my entire browsing experience to accommodate some injurious design decisions. – seagull Feb 15 '14 at 22:09
You are actually describing two annoyances. One is a straightforward "onunload", which can be countered with the userscript you provide. The other is "what if that doesn't work". Nefarious sites will use a range of techniques to subvert control of your browser. You may find an approach with the site example provided, but that doesn't mean it will work with the next site that obfuscates its methods. Whitelisting javascript managers generally involve a one-click per site you frequent, so it is quick to get back to where you were beforehand, but now with protection. – Paul Feb 15 '14 at 22:30
i really don't want to go down the route of "guilty until proven innocent", but it's certainly something to consider. thanks. – seagull Feb 15 '14 at 22:32
I hope all web browser makers allow users to control which sites can show the alert() and "are you sure to leave" as modal popup. These just leave spammers great ways to exploit users. Not only spammers, but dumb web developers in my country excessively use alert() for stupid things like "You are logged in." – Sin Jeong-hun Jan 15 '15 at 22:30

There are actually two ways to see the dialog you mention.

Only one of them is marketing spam. The sure-fire way to block that is to us NoScript or an equivalent that blocks JavaScript.

The other way you get it is, in fact, legitimate and helpful. You get it if you try and navigate away from a page where you have been adding content. This very site is a good example. If I'm halfway through typing and answer and I decide not to bother, I get a warning asking me if I am sure I want to leave. That's good!

share|improve this answer
i address this in my initial post. i don't believe it's good; i see it as an annoyance. i detest it. i wish to block them both conclusively. – seagull Feb 15 '14 at 22:36
For the 2nd part, if you havent edited anything on the page, it shouldn't be triggered. You can test this on SuperUser. I think the behaviour is baked into all browsers so you are unlikely to avoid it without some serious hacking. For the 1st issue, NoScript will always block the behaviour. – Julian Knight Feb 15 '14 at 22:45
"serious hacking" is what i come to superuser for. i want a blanket ban on this sort of behaviour because i find it user-hostile and unnecessary. if i've closed the browser, the browser should close. questions regarding operation are neither here nor there; i wish for the behaviour never to manifest itself, not for a way to alter myself to ensure i never run into it. – seagull Feb 15 '14 at 22:47
i'm not asking google to remove the feature, i'm asking if anyone knows a way to circumvent it. i think you've made this issue a little more personal than it ought to be. – seagull Feb 15 '14 at 23:20
@seagull, I feel your pain and agree 1000%. Sadly, I can't offer a perfect solution. I've used… myself. It unfortunately breaks ALL alert boxes but for me personally, I'd prefer all alerts to be broken over having onbeforeunload alerts. NOTHING is more infuriating than disobedient software. If I have a tab and I close it, I don't care if the popup is a genuine and real message: "Closing this tab will make you forfeit your entire bank balance." I clicked the X and I expect that tab to CLOSE. – Joshua Pech Nov 6 '15 at 4:36
  1. Install TamperMonkey

  2. Create a new script with the following text:

// ==UserScript==
// @name Disable Leave Page
// @namespace
// @include *

// ==/UserScript==

location.href = "javascript:(" + function() {
  window.onbeforeunload = null;
  window.onunload = null;
} + ")()";

With a little fiddling you should be able to get it to work.

share|improve this answer
interesting - i'll give it a go, cheers! – seagull Feb 19 '14 at 15:51
hi -- this doesn't work on either superuser or the spam site posted above. sorry. – seagull Feb 19 '14 at 16:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.