Browser tab close detect in gwt - javascript

I am developing a gwt application and I am going to support only for ie7. Now I have a requirement to detect browser tab close event.I have tried Window.addWindowClosingHandler(...) . But this is getting fired when we click any url in the page or refresh. I just want to detect only browser close event. I dont want to capture any other event like browser refresh, url click. Is there any way to detect only that.

it should work
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
Window.alert("bye bye beautiful");
}
});
[EDIT]
or maybe you search onBrowserEvent(); and detect which event user do
public void onBrowserEvent(Event event)
{
switch (DOM.eventGetType(event))
Event.onClick
Event.onPaste
Event.onLoad

JavaScript doesn't expose what caused the page to get closed, so GWT unfortunately cannot expose this information.
The underlying JavaScript event is the onbeforeunload event.

You could use session cookies, as described in the selected answer of this question:
GWT WindowClosingHandler firing on Browser refresh too

Related

Detecting all possible cases of page refresh

I read these two questions:
How can I detect browser tab refresh or close using javascript
and
How do I detect a page refresh using jquery?
which suggest binding to 'onbeforeunload' and also binding on F5 and Ctrl-R key presses, which is good advice.
However, most browsers have a refresh button on their address bars, like this in Chrome:
Question is: is it possible to detect and bind to refresh event of browser's address bar's refresh button?
By binding onbeforeunload to windowlike this window.onbeforeunload it should trigger in most browsers. check this fiddle. It seems it's not supported on IOS devices.
For IOS apple docs suggest using pagehide see apple page on Handling Events.
The load and unload events may not work as expected for back and forward optimization. Use the pageshow and pagehide events instead.
Keep in mind that this will also trigger on all other kinds of navigation away from the page. Such as close, tab close, back/ forward navigation, link navigation and address bar navigation
Checking if the browser is reloading versus navigating away from the page I'm pretty confident is not possible, due to security/ privacy reasons not being able to give you the destination url. You could eliminate link presses by using it is an condition when firing the onbeforeunload.
Edit: If you need to check if the page has been reloaded however, you could use sessionvariables or cookies to store that the user has already opened the page once before.

Java script code for pressing alt+f4 key

I need a java script code for a button click that press alt+f4 key of keyboard.
So I achieve the same functionality as pressing alt+F4.
So please suggest me how I can do that.
That key event is (on most OSs I guess) processed by the OS before it's even sent to the browser, so capturing it inside a browser wont help.
What u want is to replicate the window/tab close event and AFAIK the only way in Javascript to detect that kind of stuffs are onunload & onbeforeunload events.
Unfortunately those events are also fired when you leave a site over a link or your browsers back button.
You can only detect when the page is unloaded, not when the window is closed. Also, the onbeforeunload is non-standard, so it's not supported by all browsers.
I think you are looking for
window.close();

Detecting changes in window.location

I am trying to detect changes in window.location (for example to be notified if the user tries to reload the page) but I can't seem to find a way to do that.
Are there some events associated with it?
Thanks
The unload event is fired when the user leaves the page. There's also a beforeunload event in some browsers, but it's not supported in Opera.

Touch events in JavaScript on Android

I have an HTML page with a div that acts like a momentary on-off switch. It works something like this:
$('#btn').mousedown(function() {
startAction()
})
$('#btn').mouseup(function() {
stopAction()
})
$('#btn').mouseout(function() {
stopAction()
})
This works fine in a regular web browser. But it doesn't work when I load up the page in a WebView under Android. According to this, mousedown events don't work on Android like most people expect them to; so, is there any other way to accomplish this? Basically, what I want is a notification when the user puts a finger down onto the widget, and when the finger is taken away.
I'd prefer to use JQuery, but I don't have to. I'd also prefer a solution that works on other mobile platforms.
If I remember correctly, on Android, the webviews have JS disabled by default and need to be enabled. You can enable them with the document at http://developer.android.com/resources/tutorials/views/hello-webview.html
I think really, you're up to the mercy of the device and the owner as to whether or not the user has javascript enabled.
http://hubpages.com/hub/How-to-enable-disable-JavaScript-on-the-Droid-Android-phone
Hope this helps
~Kevin

Prevent browser from closing in asp.net

I am using asp.net 2.0 with c#.
i want a pop up to be displayed when user tries to close the browser and if user click on "no" [i.e. he don't want browser to be closed] then it prevent browser to get closed.
Please help.
Thanks
the code they use is
window.onbeforeunload=function() {
if (somereasonorother) return "You did not save your stuff"
}
Pointy, this is entirely possible, and it's done by many web pages for perfectly reasonable reasons.
Try something like this:
function areYouSure() {
return "Are you sure you want to leave this page?";
}
window.onbeforeunload = areYouSure;
You can try to attach yourself to the onbeforeunload event:
<body onbeforeunload="ConfirmClose();">
But I have to mention that it won't work on all browsers. The only ones that prompted something after I closed a window were Chrome, Firefox and Internet Explorer; Opera ignored the code in the JavaScript method.
This is mostly because some browsers trigger the onbeforeunload event only when you're trying to leave the current page by visiting another one, and not when you close the current window / tab.

Categories

Resources