Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block users from closing a window in Javascript?

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...

like image 358
manraj82 Avatar asked Feb 09 '10 14:02

manraj82


People also ask

How do I stop a browser window from closing?

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.

Can we disable browser close button?

No, you can't disable the close button on any web site. This would be a major security concern.

Can JavaScript close a window?

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.

What is the method for close window in JavaScript?

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.


2 Answers

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[...]

like image 170
Pool Avatar answered Oct 03 '22 12:10

Pool


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

like image 45
Imam Avatar answered Oct 03 '22 11:10

Imam