How to cancel browser close window? - javascript

Is it possible to cancel the browser close window event? Or at least add a message prompting the user whether they are sure they want to leave the page? I tried doing this with the beforeunload event, but it still closes the browser. I am looking for something similar to Microsoft's exchange website.
Thanks!

Here is an Html Code:
<html>
<head>
<script language="javascript" type="text/javascript">
window.onbeforeunload = askConfirm;
function askConfirm(){
return "You have unsaved changes.";
}
</script>
</head>
<body>
</body>
</html>
Paste this code into the text area on this page: http://www.htmlpreview.richiebrownlee.com press the button, then exit out of the page that comes up..

a ha!
It appears there's a supported JQuery solution
$(window).unload(function()
{
//...
});
http://www.webhostingtalk.com/showthread.php?t=927093
this should be cross browser supported...hopefully....

window.onbeforeunload
Will work on Safari, IE, Firefox, Chrome, but not Opera.

onbeforeunload should work fine. Bind this on body/window attribute of the html. For more details check this detailed blog post, blog

use onbeforeunload event http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

Related

When you open the Alert window in the Popup window, the popup window disappears

For IE11.
If you open the alert window in the popup window of A, the popup window disappears once, and when you check again, the popup window appears.
For other browsers (Chrome, FF, Edge), the alert appears in the popup window.
I wonder if there is a solution.
Can't reproduce the problem on my side. According to your description, I create a sample using the following sample code, it seems that everything works well:
code in main page:
<button onclick="myFunction()">Open new window</button>
<script>
function myFunction() {
window.open("popuppage.html","_blank","wight=900px, height=600px")
}
</script>
code in popuppage.html:
<head>
<meta charset="utf-8" />
<title></title>
<script>
function myfunction() {
alert("Hello");
}
</script>
</head>
<body>
<a id="btnshowalert" href="#" onclick="return myfunction();" > show alert</a>
</body>
The result as below:
Please try to clear IE browser cache and history, then create a new page to test above code or re-test your code. If still not working, you could try to reset Internet Explorer settings and share your code.

Why onbeforeunload doesn't fire if page is not clicked?

I just want to learn why is that?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>close</title>
</head>
<body>
<h1>onbeforeunload</h1>
<script>
console.log("loaded");
window.onbeforeunload = confirmExit;
function confirmExit(){
console.log("closed");
return false;
}
</script>
</body>
</html>
If you copy and save this code as a .html file and run it in browser and don't click inside the page and close the tab or browser, then the onbeforeunload won't fire!
But if you click once inside the page, and then try to close the page, it works all fine as it should.
This is documented behaviour:
Note: To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with; some don't display them at all. For a list of specific browsers, see the Browser_compatibility section.
Try this:-
$(document).ready(function() {
console.log("loaded");
window.onbeforeunload = confirmExit;
function confirmExit(){
console.log("closed");
return true;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>onbeforeunload</h1>

Inject JavaScript after refresh

I would like to my script work after website refresh. I have JavaScript code that is injecting to webbrowser.
string element = "alert(\"hello\")";
form1.webBrowser1.Document.InvokeScript("eval", new object[] { element });
Shamelessly stolen from here:
"You don't want the refresh, you want the onbeforeunload event.
http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
Sample code from article"
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

Browser close event For Firefox not working

In my application i have to alert user to signout on browser close.
For that i have used the javascript as below
<body scroll="no" onbeforeunload="browerClose()">
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://localhost:8086/egs/ervlet?pri=logOut";
}
this is Working for IE ,but not works for FireFox,
Wats the Problem....Any Suggesstions
Thankxx in Advance,
I would suggest you move the javascript function to the head section of the HTML document. That way it is working for Firefox. Maybe this is because HTML documents are processed in sequential order and you need to define the function before you can use it.
Do something like this:
<head>
<script language="JavaScript" type="text/javascript">
function browerClose()
{
window.alert("Click OK to SignOut");
window.location.href = "http://google.com";
}
</script>
</head>
<body scroll="no" onbeforeunload="browerClose()">
<!-- you code here -->
onbeforeunload event will not work from fire fox version 26 if u used any custom alert message in your application which means you need to perform x operation when closing browser/tab or refreshing page but you put alert message in function which will be called from onbeforeunload then the x (update) operation will not happened.
<html>
<head>
<script>
function unloadfunction() {
alert('test');
// update employee ID in Database
}
</script>
</head>
<body onbeforeunload="unloadfunction();">
</body>
</html>

How to close the window with Javascript

Haven't been programming in JS for a while.
Now, I have following thing:
<html>
<head>
<script language="JavaScript">
function enlarge()
{
window.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>
(it's very simplified, as, in fact, I have WordPress platform with custom JS modifications etc, but in general, this is the idea).
I don't understand why it's not working.
JavaScript cant close the window unless it opened the window.
In your function, replace window.close() with alert('here') and you'll see the function works fine.
If you want your function to close a window, first open one:
<html>
<head>
<script type="text/javascript">
var popup;
function closewin()
{
popup.close();
}
function openwin()
{
popup = window.open('http://www.google.com');
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onclick="openwin()" /> Click to open, then come back here
<br><br>
<img src="addresstomyimg.png" onclick="closewin()" /> Click to close
</body>
</html>
Demo: http://jsfiddle.net/AlienWebguy/pbFha/
If you try running this script using Firefox and use the Firefox's Error Console to look for errors, you can see that the following error gets logged when you run this script.
Scripts may not close windows that were not opened by script.
You can launch the Error Console in Firefox by pressing Ctrl + Shift + J.
On Chrome, your script successfully closes the tab in which it is running.
Note that the right way to use the <script> tag while writing JavaScript is as follows:
<script type="text/javascript">
If you're trying to close the main window, it won't work.
You can only close windows that were opened by JavaScript.
This code works on IE.
<html>
<head>
<script language="JavaScript">
function enlarge()
{
self.close();
}
</script>
</head>
<body>
<img src="addresstomyimg.png" onClick="enlarge()" />
</body>
</html>

Categories

Resources