views:

187

answers:

8

Is it possible to block users from closing the window using the exit button [X]? I am actually providing a close button in the page for the users to close the window.Basically what im trying to do is to force the users to fill the form and submit it.I dont want them to close the window till they have submitted it.

guys i really appreciate your comments,im not thinking of hosting on any commercial website.its an internal thing,we are actually getting all the staff to participate in this survey we have designed.... i know its not the right way but i was wondering if there was a solution to the problem we have got here...

A: 

Well you can use the window.onclose event and return false in the event handler.

function closedWin() {
    confirm("close ?");
    return false; /* which will not allow to close the window */
}
if(window.addEventListener) {
     window.addEventListener("close", closedWin, false);
}

window.onclose = closedWin;

Code was taken from this site.

In the other hand, if they force the closing (by using task manager or something in those lines) you cannot do anything about it.

jpabluz
I don't believe this works with many browsers anymore.
Nick Berardi
If it *does* work I'd call it a bug in the browser.
camccann
+6  A: 

What will you do when a user hits ALT + F4 or closes it from Task Manager

Why don't you keep track if they did not complete it in a cookie or the DB and when they visit next time just bring the same screen back...:BTW..you haven't finished filling this form out..."

Of course if you were around before the dotcom bust you would remember porn storms, where if you closed 1 window 15 others would open..so yes there is code that will detect a window closing but if you hit ALT + F4 twice it will close the child and the parent (if it was a popup)

SQLMenace
+4  A: 

This is generally a good way to get on your user's nerves. I suggest you follow a different workflow. Users don't appreciate a website that messes with their expected behavior.

Razvan Caliman
guys i really appreciate your comments,im not thinking of hosting on any commercial website.its an internal thing,we are actually getting all the staff to participate in this survey we are designing....
manraj82
A: 

It's poor practice to force the user to do something they don't necessarily want to do. You can't ever really prevent them from closing the browser.

You can achieve a similar effect, though, by making a div on your current web page to layer over top the rest of your controls so your form is the only thing accessible.

Jonathon
Ugh that's also extremely annoying. It's a great way to ensure I don't come back to a site.
FrustratedWithFormsDesigner
It is annoying, I agree. But it's better than forcing me to close my browser with a task manager. I just close the annoying tab, instead.
Jonathon
+1  A: 

Whatever your reason for wanting to do this, please don't do it.

Tilo Mitra
+5  A: 

Take a look at onBeforeUnload.

It wont force someone to stay but it will prompt them asking them whether they really want to leave, which is probably the best cross browser solution you can manage. (Similar to this site if you attempt to leave mid-answer.)

<script language="JavaScript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        return "You have attempted to leave this page. Are you sure?";
    }
</script>
Pool
Now this isn't so bad!
FrustratedWithFormsDesigner
This will not work in Opera, as it doesn't fire onbeforeunload events.
Marcel Korpel
*shakes fist* at Opera.
Pool
A: 

This will pop a dialog asking the user if he really wants to close or stay, with a message.

var message = "You have not filled out the form.";
window.onbeforeunload = function(event) {
    var e = e || window.event;
    if (e) {
        e.returnValue = message;
    }
    return message;
};

You can then unset it before the form gets submitted or something else with

window.onbeforeunload = null;

Keep in mind that this is extremely annoying. If you are trying to force your users to fill out a form that they don't want to fill out, then you will fail: they will find a way to close the window and never come back to your mean website.

Gipsy King
+2  A: 

If your sending out an internal survey that requires 100% participation from your company's employees, then a better route would be to just have the form keep track of the responders ID/Username/email etc. Every few days or so just send a nice little email reminder to those in your organization to complete the survey...you could probably even automate this.

jlech
I'm pretty sure there are many pre-built survey systems that are already capable of this (not sure if they are a lot of $$, but that's another topic).
FrustratedWithFormsDesigner
we were actually depending on this prebuilt survey system,but it has proved to be useless...so was thinking of an easier way of doing it,but anyway thanks for your comments....
manraj82