Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 at 17:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You could whitelist javascript for only those sites you want it to run on, which would essentially solve this problem. –  Paul Feb 15 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 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 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 at 22:32
You can consider blocking spammy sites altogether by making an entry in your HOSTS file (assuming you're using Windows). The file is typically at this path - C:\Windows\System32\drivers\etc. Open the hosts file with administrative privileges & make an entry for the offending site like this - unwanted.domain –  mvark Feb 16 at 4:26

2 Answers 2

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 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 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 at 22:47
Sure. The other way would be to use the onunload script in conjunction with javascript blacklisting for the sites that have workarounds. –  Paul Feb 15 at 22:51
@seagull, SuperUser is not for serious hacking, there are other forums here for that. SuperUser is for users. What you don't like, others do. Go complain to the browser creators. –  Julian Knight Feb 15 at 22:54
  1. Install TamperMonkey

  2. Create a new script with the following text:

// ==UserScript==
// @name Disable Leave Page
// @namespace http://www.greasespot.net/
// @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 at 15:51
hi -- this doesn't work on either superuser or the spam site posted above. sorry. –  seagull Feb 19 at 16:52

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