How to block users from closing a window in Javascript?
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:
- Preventing users from inadvertently losing data.
- 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[...]
How to prevent closing browser window?
Keep your code as is and use jQuery to handle links:
$(function () {
$("a").click(function {
window.onbeforeunload = null;
});
});
Prevent Window Close
There is no way to completely stop it, the best you can do is confirm the leaving.
window.onbeforeunload = function (e) {
var message = "Your confirmation message goes here.",
e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}
// For Safari
return message;
};
Answer from Intercept page exit event
About alt+f4 refer to this: Disable ALT+F4, yes I know it isn't recommended
Prevent user to close tab or browser window
Is that possible to start a loop on window.onbeforeunload to open same
current page again and again on tab exit?
Yes.
Why Its not possible?
but browser blocking it as popup opening
Your popup blocker is turned on at the browser you are testing the javascript
at to prevent this browser behaviour by default; or by the setting that you have implemented.
Solution: Turn off the popup blocker at the browser that you are viewing https://jsfiddle.net/q9kd4b0x/ at .
A new window
at a new tab
should open at each onbeforeunload
event
Is there any way to prevent the browser closing after clicking on the Close button
Due to security reasons this is not possible!
You can show a confirmation dialog using onbeforeunload
(see e.g. here for how to do it), giving the user the choice to not leave the page after all. But you can't prevent the closing against the user's will.
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
Prevent a user from closing a browser window using X?
Why do you want to do this? We can probably help you come up with a different design that doesn't require this if you tell us what you're trying to do.
However, to answer your question: it's not possible to catch that event in all cases. You cannot prevent the user from closing the browser or guarantee that your code will execute when they do. You can make it slightly annoying for them, but they can disable javascript, or kill the process, or reboot the computer. Using the unload function is the closest you can come to having some code that runs when the window closes (it will run in cases of normal shutdown or when the user navigates away).
Prevent accidental close of browser window while submitting
Situation solved!
I removed the submit-functionality of the button and replaced it with this:
<input type="button" name="button" id="button" value="Send video" onclick="sendInformation();" />
I then created a javascript, which was submitting the information, but at the same time, initializing the onbeforeunload-code.
document.getElementById("pleasewait").style.visibility = 'visible';
document.getElementById("myForm").submit();
window.onbeforeunload = function(e) {
return "Please make sure, the video has finished uploading before closing this window.";
};
It also has the undesired effect, that the onbeforeunload-code is invalidated, the moment the upload.php page has loaded completely - so the user isn't asked twice when closing a succesful upload.
It works flawlessly.
Related Topics
R Networkd3 Package: Node Coloring in Simplenetwork()
R Shiny Build Links Between Tabs
Template Language That Works on Both Server and Client
How to Detect Emoji Using JavaScript
Creating Range in JavaScript - Strange Syntax
Console.Log() After Setstate() Doesn't Return the Updated State
Difference Between Date(Datestring) and New Date(Datestring)
Jquery UI - Close Dialog When Clicked Outside
Deprecation Warning in Moment.Js - Not in a Recognized Iso Format
React - User-Defined Jsx Components Not Rendering
Xmlhttprequest Status 0 (Responsetext Is Empty)
Merge Keys Array and Values Array into an Object in JavaScript
Sort an Array of Object by a Property (With Custom Order, Not Alphabetically)
Calculating Page Load Time in JavaScript
How to Obtain the Query String from the Current Url with JavaScript
Getting Title from Wkwebview Using Evaluatejavascript
What Does It Mean That JavaScript Is a Prototype Based Language