Some way to detect close tab/browser for all browsers? - javascript

There are some way of detecting a close event from tab/browser without close when i click in other links in page or forms and valid for all browser, i tested "beforeunload" and other replies about this topic here and not working, i am testing in Firefox and Chrome
This is not working :
window.onbeforeunload = function (event) {
var message = 'Important: Please click on \'Save\' button to leave this page.';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
};
Same when i close the tab or the browser is doing nothing; but yes when i click a link

Related

Remove page refresh browser popup

I added page beforeunload and unload functions as follows:
window.onunload = function (e)
{
//mycode1
return '';
};
window.onbeforeunload = function(e){
//mycode2
return '';
}
and they are getting called after browser confirmation popup occurs. But now I want to unbind these functions. I tried
window.onunload = null;
window.onbeforeunload = null;
But seems like they are not working and still browser refresh confirmation popup appears.

Stopping the user from navigating away using `window.onbeforeunload`

I am trying to display an alert when the user clicks the back, forward or refresh browser buttons, but I am not getting the desired output...
<script type="text/javascript">
$(document).ready(displayAlert());
function displayAlert() {
window.onbeforeunload = function () {
return "Are you sure want to LOGOUT the session ?";
};
}
</script>
WindowEventHandlers.onunload The unload event is raised when the window is unloading its content and resources. The resources removal is processed after the unload event occurs.
window.onunload = funcRef;
WindowEventHandlers.onbeforeunload An event that fires when a window is about to unload its resources. The document is still visible and the event is still cancelable.
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
IE has issues with onload event and Opera has with onbeforeunload. So to reach to a solution which would handle both the situations I came across user3253009 answer
/*Code Start*/
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = 'Cookies for you.. If you stay back!!?'; // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
/*Code End*/
Gist. Hope it helps!
Update
If you want to show a Bootstrap Modal when user is navigating away from you page,then you can try something like below:
window.onbeforeunload = function(event) {
event.preventDefault();
$('#cancel_modal').modal('show');
};

onbeforeunload keeps poping up an alert box when user click something

I am created a page that warns the user when they click on the (close x) button on the window. I did some reading and discovered that JavaScript had a function called onbeforeonload which can take of the job I was trying to achieve. I however found at after my implementation that, when a user clicks on anything in my window (example: save and enter) The dialog box reappears. I was wondering how I could only target the specific X button in the window.
window.onbeforeunload = function (evt) {
var message = 'Do you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
Right now the function is being called globally... this resource might help you achieve what you are looking for: http://randomdrake.com/2009/09/23/how-to-use-onbeforeunload-with-form-submit-buttons/
This is a "working as intended" behavior for IE. Anchor tag clicks, regardless of whether they navigate or not, will trigger the onbeforeunload event.
This is the workaround I used - I am not sure whether it is the best approach or not:
document.onmouseup = function () {
if (window.event.srcElement.tagName === 'A') {
// turn off your onbeforeunload handler
...
// some small time later, turn it back on
setTimeout(..., 200);
}
};

I want to call function on window unload

I am trying to display confirmation box using window.confirm on window unload event.
If a user clicks on the OK button on confirmation box then I want to call one function and if user clicks the CANCEL button then window should be get closed.
My code is:
<script>
function confirmit(){
var result=window.confirm("Are you sure?");
if(result) {
// close all child windows
} else{
// window should not get close
}
}
</script>
<body onunload='confirmit();' >
But the problem is if I click on CANCEL button, window is getting closed.
Please help me.
You can't prevent unload to stop the page from unloading. You need to bind to onbeforeunload instead. You should just return the string you want to display to the user from the event handler (note that in some browsers the string may not be displayed)
<script type="text/javascript">
window.onbeforeunload = function(e){
var msg = 'Are you sure?';
e = e || window.event;
if(e)
e.returnValue = msg;
return msg;
}
</script>
More info here
JSFiddle Example here
change your code to this to make it work cross-browser:
<script>
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Do you really want to exit?';
}
// For Safari
return 'Do you really want to exit?';
};
</script>
<body>
...
note that this is using the onbeforeunload-event (more information / view an example) where the return-value has to be the message that should be shown to the user.
i don't know if you'll have a chance to react on the confirmation to do something after that (closing child-windows for example), but i don't think so.

How to observe the location of a opened Popup?

I wanted to open an popup and get notify if the user closes the Popup or leaves it through an external link.
popup = window.open(
"http://ec.europa.eu/yourvoice/ipm/forms/dispatch?form=tobacco6",
'',
'status=no,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,copyhistory=no'
);
To check if the user closed the popup I set an Timeout that checkes if the popup is open.
if(popup.closed)
How can i check if the location of the popup is still the inital one?
popup.location
just return about:blank.
You can simply use the onbeforeunload event.
popup.onbeforeunload = function(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}

Categories