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...
Use the Pin tab option to keep Prevent Close available in Chrome. To make this as painless as possible, I recommend pinning this website to your browser then moving the tab out of the way. To do that open Prevent Close, and then right-click the tab with your mouse. From the context menu select Pin tab.
No, you can't disable the close button on any web site. This would be a major security concern.
JavaScript does not allow one to close a window opened by the user, using the window. close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script.
close() The Window. close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.
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:
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[...]
If you don't want to display popup for all event you can add conditions like
window.onbeforeunload = confirmExit; function confirmExit() { if (isAnyTaskInProgress) { return "Some task is in progress. Are you sure, you want to close?"; } }
This works fine for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With