Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  82] [ 5]  / answers: 1 / hits: 144199  / 15 Years ago, tue, february 9, 2010, 12:00:00

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 I'm trying to do is to force the users to fill the form and submit it. I don't want them to close the window till they have submitted it.


I really appreciate your comments, I'm 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...


More From » dom-events

 Answers
123

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>

Edit: Most browsers no longer allow a custom message for onbeforeunload.


See this bug report from the 18th of February, 2016.



onbeforeunload dialogs are used for two things on the Modern Web:




  1. Preventing users from inadvertently losing data.

  2. Scamming users.



In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.




Firefox already does this[...]



[#97620] Saturday, February 6, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacie

Total Points: 476
Total Questions: 92
Total Answers: 102

Location: Bosnia and Herzegovina
Member since Tue, Mar 29, 2022
2 Years ago
stacie questions
Fri, Jun 26, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Fri, Aug 2, 19, 00:00, 5 Years ago
;