I want to make popup which freezes the browser until the popup is closed. But I am stuck at a point since on closing the browser, it is asking the user to stay or leave but when a user clicks on leave it shuts down. I instead want a that when a user clicks the leave option it either reload the site or do nothing, also on doing the same for second time Chrome is giving alert to prevent any additional dialogue which I want to disable or it should do nothing. Until the popup is closed. So at last users should not be able to close their browser from a closing button of browser or by going back or replacing the URL. It should freeze until the requirement of the popup is fulfilled. Thank you and please help.
-
Only chance is to write your own browser. :) Noone wants popups my friend! Really noone. – kwoxer Jun 14 '17 at 08:48
2 Answers
You can't force the users to stay on your site as the browser doesn't allow this (think of a malicious site forcing you to stay there). Your approach is probably the best you can do. Show a dialog that says something like. "Please wait while exiting..." or similar.
If you want to hide the "Block additional dialogs from this page" thing you can create your own dialog. Just a div with position: fixed and display: none and then onBeforeUnload show it with display: block. Here is more on that: Stackoverflow.com

- 5,233
- 1
- 20
- 38
-
-
Glad I could help. You may upvote and accept it as answer so that others can see what worked for you. If you need further help just comment :) – Spitzbueb Jun 14 '17 at 09:07
-
My problem is still not solved. I want to disable back button when my popup appears. Its is a alert box which appears and i want to disable the feature to close the browser unless the popup which is a JavaScript alert box closes. – John Jun 16 '17 at 10:18
-
You didn't read my answer well. You **can't** stop the browser from closing. If the user wants to leave, he leaves. Browsers don't allow Javascript to interrupt that. Think of a site that blocks you from closing the browser but you as user can't turn it off. This would be a huge security problem. – Spitzbueb Jun 16 '17 at 10:45
In that case you can use bootstrap popup: like this:
https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h
and on the click event on close button you can add new alert or some other popup.. and closing this type of popup box cannot reflect other data..
It might helps you.!

- 392
- 2
- 11
-
Thankyou for your answer. Can you provide me a code to disable close button of browser when popup opens – John Jun 15 '17 at 17:55
-
How can i prevent user from closing brower when my popup appears. It is a JavaScript alert box. I want that the user cannot close the brower using close button when the box appears. – John Jun 16 '17 at 10:21
-
Actually we can not detect browser event..this is not in our control..but as I suggested you in my answer you can use a model popup.. – Hitarthi Panchal Jun 16 '17 at 11:20
-
Is there any way i can replace onbeforeunload default dialog box and add custom box which can then prevent user from leaving? – John Jun 16 '17 at 13:11