I'm trying to debug an issue where the browser disappears after refreshing a particular page. I want to see if I can eliminate JavaScript as being the cause of this issue.
Does IE have an equivalent to the Firefox configuration option dom.allow_scripts_to_close_windows?
I did some googling and this was the closest thing I could find, but I do not know if it also prevents JavaScript from closing windows.
Prevent Users from Closing the Browser or Explorer Windows
Edit: You can use the onbeforeunload event to see if the page is triggering some sort of close event.
window.onbeforeunload = function() {
return "The browser is about to close this window, are you sure?"; // Will appear as a confirmation box
};
Related
I often accidentally close my Chrome browser and have to reopen and reload all the tabs that I'd been working on. Since Chrome does not a have built in confirm before closing mechanism, I wrote a simple page to ask for confirmation before closing. I leave that page open among my other tabs.
<!DOCTYPE html>
<html>
<body>
<p>This page is to prevent accidental closing of Chrome.</p>
<script language="JavaScript">
window.onbeforeunload = function () {
return "Are you sure?";
};
</script>
</body>
</html>
Recently I updated my Chrome browser from version 56 to 60. Now the code doesn't seem to work as it no longer asks for confirmation before closing. I tried many different variations from the internet but none seem to work.
Note: I am very new to web development.
According to the MDN docs :
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.
So, I think your function is not guaranteed to run, especially because
Chrome 60 explicitly has that first behaviour. From the notes:
From Chrome 60 onward, the beforeunload dialog will only appear if the frame attempting to display it has received a user gesture or user interaction (or if any embedded frame has received such a gesture).
So if you want to continue using this approach, you may need to interact with the page at some point in your session.
Alternatively, to open all the tabs you had open when you restart Chrome, try pressing Ctrl-Shift-T.
window.onunload = function()
{
confirm("close the window?")
}
Why don't I have the confirm window coming out when closing a window?
Presumably it is to prevent pages from repeatedly preventing you from closing a page. With the beforeunload event, you can, however, get what is, in most modern browsers, a non-customizable or only partly customizable dialog prompting the user whether to leave or not.
Many browsers not support window.unload.
Please view this link.
window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?
Most browsers prevent the use of alert/confirm in onunload.
Instead use an onbeforeunload handler that returns a string:
window.onbeforeunload = function() {
return "Don't go!";
};
This will then be presented to the user in a dialogue box.
See also: window.unload() won't work in jQuery
I have MyPage.aspx html page (generated using ASP.Net). When user tries to navigate away from this page, I need to close the window – user should not be able to go back or navigate to another page.
When I used window.close() inside window.onbeforeunload event, it asks for a confirmation to the user. “The webpage you are viewing is trying to close the window. Do you want to close the window?” On clicking “No” the user can escape the close attempt. Is there any way to forcefully close the window without giving an option to the user?
Reference:
How can I close a browser window without receiving the "Do you want to close this window" prompt?
Html javascript to open new window and close current window
"Unknown Exception" when cancelling page unload with "location.href"
Display confirmation popup with JavaScript upon clicking on a link
You can "trick" the browser like this:
window.onbeforeunload = function() {
window.open('', '_self', '');
window.close();
}
It seems to work in chrome/safari/ie/ff: http://jsbin.com/olijig/1
Firefox seems stubborn, but there might be another way to do the same in FF.
I should probably say that this technique is in no way standard and I don’t recommend it at all, and this code might break in many browsers besides firefox.
UPDATE
It actually works in Firefox too (latest version), but not older versions (I tried 3.6.1). You need to do some more testing to confirm the browser compatibility.
No, you can't. The user must be always capable of controlling whatever happens in his browser.
I'm not positive about this, but I believe if you have a window open another window, the parent window can close that child window. Would it be practical to have a landing page that opens your app in a separate window that could then close the window through javascript? Someone can probably elaborate more, as I haven't done this myself.
What are the differences between onbeforeunload and onunload ?
Also I have a specific question related to it's use on the iPad...I have a page (myPage.html) where I am trying to show an alert when the page is closed (i.e. X is pressed to close the tab on iPad)
Now I tried using both window.onunload and window.onbeforeunload
Below are my findings on the iPad;
Using window.onunload , I am able to get an alert when user navigates to a different page from myPage.html (either by clicking on some link or doing a Google search while on myPage.html) . However nothing happens when the tab is closed from the minimized view (X)
Using window.onbeforeunload, I neither get an alert even if the user navigates to a different page from myPage.html OR if he closes the tab (X) from the minimized view.
I wanted to know if there is any alternate way to fix this issue ?
Thank you.
onunload is responsible for executing an instruction when the page is closed. It also causes issue with IE and AJAX.
onbeforeunload is more efficient because it does not run in competition with the actual closing of the window and is triggered before onunload
I know Opera used to not acknowledge onbeforeunload - not sure if they've fixed that, but I always register the listener for both to be safe:
window.onunload = window.onbeforeunload = (function(){...
Adding with AlienWebguy's ans, to avoid dual calls at the browsers that support both events,
var onBeforeUnLoadEvent = false;
window.onunload = window.onbeforeunload= function(){
if(!onBeforeUnLoadEvent){
onBeforeUnLoadEvent = true;
//your code here
}
};
onbeforeunload:
Called before unloading begins
MDN tells me you can cancel the closing of the page using event.preventDefault();
or by returning a non-void value (ie a value != 0), the page will pop up a confirmation dialog that allows the user to choose to cancel the close
MDN also says Opera 12 and up support onbeforeunload - looks like its supported it for a while now
onunload:
Called after unloading has begun, but before any resource deallocation (its not clear to me what exactly is done during this period)
Its too late to cancel the page close at this point
The only reason I can think of why you would want to use onunload over onbeforeunload would be where your onunload code could take some significant time, and don't want the user to see a window that hangs while closing.
One significant difference (other than cancelability) between the onbeforeunload and onunload is that the former is triggered for download links and the latter is not. Example: download will trigger the onbeforeunload handler, but not the onunload.
I'm trying to open a new window like so:
$('#wrapper').click(function() {
window.setTimeout(function() {
//alert('hi');
window.open("http://example.com", "ExternalLinks", "resizable=yes, scrollbars=yes, status=yes");
}, 1000);
});
This works in Firefox, but not in Chrome or Safari (so far, I've just tested on a Mac). The alert() works in all browsers, so there seems to be something preventing the window.open from executing in Safari/Chrome. Furthermore, if I remove the setTimeout and just call the window.open then it does work in all 3 browsers. It's almost like if the window.open is nested too far away from the click event, then it doesn't work in Safari/Chrome.
So you know, I have an all-Flash website and I'm trying to get external links to open in a new window, so I'm reading the hash tag in the URL (ex. htp://example.com/#/facebook/) and if it matches certain items, then I'm calling window.open to open a specific URL. I don't have access to the Flash source, or I would handle this there.
Any ideas?
Safari/Chrome have built-in pop-up blockers that stop this from working. The only javascript that is allowed to open a new window in Safari/Chrome is javascript directly attached to click handlers (and other direct user input handlers). In past versions people figured out some ways to cheat (like generating some other element -- a form or div -- and simulating user input with javascript), but newer versions are smarter about detecting this. I'd recommend re-configuring things so that you don't use a delayed pop-up -- that is the kind of thing that can generally be jarring to a user after all.
I got around this by checking the return value of window.open() for undefined. If that is true call alert() with a message for the user to to disable their popup blocker.
var myWin = window.open([args]);
if (myWin == undefined)
alert('Please disable your popup blocker');
Another workaround
Just open a popup with ACCEPT and CANCEL options and attach the window.open action to the ACCEPT button and it will works. It worked for me...