I would like to know if there is any way to handle the browser exit event.
For example, I would like to send a query when the user click on the cross-exit or simply close his browser.
I think I have found a solution. I haven't tested it on IE but it seems to be working on Firefox :
Ext.EventManager.on(window, 'beforeunload', function() {
alert('cross-exit tab click AND cross-exit browser click');
});
Ext.EventManager.on(window, 'unload', function() {
alert('cross-exit tab click');
});
PS : Thank you exhuma for having edited the question ;)
This thread might shed some insight:
http://forums.devarticles.com/javascript-development-22/how-to-stop-browser-from-closing-using-javascript-8458.html
I haven't given this solution a test-run yet. But it seems possible.
Here's a more in-depth discussion on the onbeforeunload event handler.
http://bytes.com/topic/javascript/insights/825556-using-onbeforeunload-javascript-event
And then there's always quirksmode. Unfortunately the load/unload events are not yet covered by quirksmode. As stated on the page:
I haven’t tested the load and unload events yet; they’ll be added to the table later on.
Related
I am trying to fetch all events (maximized, maximize etc). I have a suitable code for this from this link How to Detect Window On Minimize/Maximize Event in Chrome Extension?.
But the problem when switch tab (using alt+tab) window.chrome.onFocusChanged listener is not firing.
My code :
chrome.windows.onFocusChanged.addListener(function(windowId) {
console.log("focus change", windowId);
});
Is there a solution for this or... it is a bug?
This is a confirmed bug which was already posted at the Chrome Bug Tracker back in 2014. Unfortunately, it received little to no attention. No news if it will be fixed in milestone iterations in the near future either.
I use a jQuery Dirty Forms plugin in my web-application like this:
$(document).ready(function(){
$('form.checkUnsavedData').dirtyForms();
});
$(window).on('beforeunload', function(){
if($.DirtyForms.isDirty()){
return($.DirtyForms.message);
}
});
But first click by any link doesn't trigger onbeforeunload event. Only the next clicks trigger it. This problem keeps in different browsers (google chrome, firefox, ie10). What's the matter?
I believe you will need to add the onBeforeUnload API As it says in the drupal Download & extend or else you should try not to bind the beforeunload through jQuery and do it with normal javascript like: window.beforeunload = function(){/*code*/};. Finally check what jQuery version are you using, what browsers and how they handle this event. Check out Catching the Javascript beforeunload event, the cross-browser way it's a little dated but it may help you.
What's the matter?
Dirty Forms automatically attaches to the beforeunload event. You are doing it again. This is likely causing your problematic behavior. You should change your code to just...
$(document).ready(function(){
$('form.checkUnsavedData').dirtyForms();
});
How is this site triggering its popup while bypassing Chrome's pop-up blocker?
http://undertexter.se/
I thought pop-up blockers only allowed window.open if it was triggered by a user action but that's not the case here so how are they doing it?
OUTDATED
Origin resources and fiddles do not work anymore.
I figured out that there is an popup opening in chrome if you visit the Website first time (empty cache) and click somewhere on the document.
After a refresh and a click on it again nothing will happen if cache wasn't removed.
So lets start the game of reverse engineering...
Took the script from
http://undertexter.se/ and startet refuscation.
After a few steps I got the following code and I think this is not planned by browser manufactures to support something like this.
Now I wish you a lot of luck to use that for your own but I think it's evil.
Look on js fiddle for the result:
Its the reverse of:
http://www.wigetmedia.com/tags/undertexter.se.js
http://jsfiddle.net/W9BdS/
Tested on my own server, This opens up http://google.com in a new (standalone) window in Chromium 28 on Ubuntu 12.04. Adblock is active.
<script type="text/javascript">
document.addEventListener('click', function() {
window.open('http://google.com','...','width=1080 height=640')
})
</script>
try this using JQuery
$('#yourCotrolId').on('click', function() {
window.open('yourLink','...','width=1080 height=640')
});
window.open is blocked by modern browsers when called not in a click event handler.
I faced this myself while using Rails' format.js responses and wrote a small plugin that makes showing JS popups as simple as calling window.open():
Popup("<div>Hello world</div>").show('up')
You could use the .trigger method for simulating an user action:
Click
$('a').trigger('click');
.trigger() can not be used to mimc such native browser events, you can use .simulate() from simulate.js to mimic such events.
include simulate.js in your file and use,
$('a').simulate('click');
Does anyone know if the onbeforeunload event is supported on the iPad and/or if there's a different way to use it?
I've tried pretty much everything, and it seems like the onbeforeunload event is never triggered on the iPad (Safari browser).
Specifically, this is what I've tried:
window.onbeforeunload = function(event) { event.returnValue = 'test'; }
window.onbeforeunload = function(event) { return 'test'; }
(both of the above together)
window.onbeforeunload = function(event) { alert('test')'; }
(all of the above functions but inside <body onbeforeunload="...">
All of these work on FF and Safari on the PC, but not on the iPad.
Also, I've done the following just after loading the page:
alert('onbeforeunload' in window);
alert(typeof window.onbeforeunload);
alert(window.onbeforeunload);
Respectively, the results are:
true
object
null
So, the browser does have the property, but for some reason it doesn't get fired.
The ways I try to navigate away from the page are by clicking the back and forward buttons, by doing a google search in the top bar, by changing location in the address bar, and by clicking on a bookmark.
Does anyone have any idea about what's going on? I'd greatly appreciate any input.
Thanks
This bit of JavaScript works for me on Safari and Chrome on ipad and iphone, as well as desktop/laptop/other browsers:
var isOnIOS = navigator.userAgent.match(/iPad/i)|| navigator.userAgent.match(/iPhone/i);
var eventName = isOnIOS ? "pagehide" : "beforeunload";
window.addEventListener(eventName, function (event) {
window.event.cancelBubble = true; // Don't know if this works on iOS but it might!
...
} );
I have found that the onunload() event does fire. It's behavior is somewhat odd; whatever you have in your callback function attached to the event is actually run after the new page has loaded in the background (You can't tell it's loaded yet, but server logging will show that it has).
More oddly, if you have a confirm() call in your onunload(), and the user has clicked a link to go somewhere else, you are in business. If, however, the user closes the iPad Safari browser tab, the onunload() event will fire, but your confirm() will have an implicit cancel as response.
Only Apple would know for sure, but my guess is that they purposely did not enable that functionality in mobile Safari because it is most often used by shady characters to get you to stay on their site or pop up lots of porn/advertising windows.
There's a known bug in WebKit with onbeforeunload. I believe it's fixed in the latest beta of Chrome 5, but it's quite possible the iPad's browser is made from a version of WebKit that doesn't have the fix.
Related Chrome bug report.
beforeunload event is not supported by Mobile Safari. You can see the list of all supported events here: Handling Events Apple documentation
And the beforeunload is not in the list!
https://code.google.com/p/chromium/issues/detail?id=97035
see hear.
alerts are no longer allowed during page dismissal events (beforeunload, unload, pagehide).
I think alerts, prompt, confirm, and other actions like these are also no longer allowed.
Here's a solution that should work on all modern browsers:
var unloaded = false;
window.addEventListener("beforeunload", function(e)
{
if (unloaded)
return;
unloaded = true;
console.log("beforeUnload");
});
window.addEventListener("visibilitychange", function(e)
{
if (document.visibilityState == 'hidden')
{
if (unloaded)
return;
unloaded = true;
console.log("beforeUnload");
}
});
Mobile browsers don't tend to not support beforeunload because the browser can go into the background without unloading the page, then be killed by the operating system at any time.
Most desktop browser contain a bug that causes visibilityState to not get called when the document unloads. See: here.
Therefore, it's important to include both events to cover all scenarios.
NB
I have used console.log instead of alert in my example because alert will get blocked by some browsers when called from beforeunload or visibilitychange.
If you just need to know if the page has been left you can use document.unload. It works fine in ios browsers. If you see on Apple documentation you'll find that it's deprecated and they recommend to use document.pagehide
This has / may have been asked before, but, as far as I remember, the question was not satisfactory answered at that time ^^
How can I register a window or tab closing event with Javascript? I have tried body.onclose and body.onunload, and dozens others whose names I made up myself and thought they might possibly exist, but none of it worked, or, if it did, it only fired after the window or tab has been closed.
Question:
Is there any way to register such an event before the window or tab has been closed? It needn't even be compatible with all browsers, as long as it works with Firefox.
window.onbeforeunload= …;.
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
WE don't have Onclose but we have OnbeforeUnload. Even I am looking for same event but no luck.
Unload and beforeUnload have negative effects on the "page cache" (also known as the "back forward cache"). This is a special cache that preserves some extra state when you navigate away from the page, so that it can be seamlessly resotred when you navigate 'back' (or unclose a tab). If you've ever wondered why some form fields keep their contents when you click back and some don't - this is often part of the reason.
You should probably take a look at the "pageshow" and "pagehide" events in Firefox and
WebKit. IE doesn't have a page cache equivalent, so in that environment you should be fine to use before/unload. Despite Opera's early concerns, pageshow and pagehide made it into the HTML5 Spec