Intercept Page Exit Event

Intercept page exit event

Similar to Ghommey's answer, but this also supports old versions of IE and Firefox.

window.onbeforeunload = function (e) {
var message = "Your confirmation message goes here.",
e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}

// For Safari
return message;
};

Intercept page exit except from button click in flash/as3

You need to use ExternalInterface. In your JavaScript
code add this method:

function unsetBeforeUnload(){
window.onbeforeunload = null;
}

And now in your ActionScript code in "stop" button click handler method add this line:

 ExternalInterface.call("unsetBeforeUnload");

That's all. May be you'll need to add this line to your Flash object : <param name="allowScriptAccess" value="always" /> in case you don't have it now.

How to call a function before leaving page with Javascript

You can always call your function before leaving the page.

function myfun(){
// Write your business logic here
console.log('hello');
}

onbeforeunload:

window.onbeforeunload = function(){
myfun();
return 'Are you sure you want to leave?';
};

Or with jQuery:

$(window).bind('beforeunload', function(){
myfun();
return 'Are you sure you want to leave?';
});

This will just ask the user if they want to leave the page or not, you cannot redirect them if they select to stay on the page. If they select to leave, the browser will go where they told it to go.

You can use onunload to do stuff before the page is unloaded, but you cannot redirect from there (Chrome 14+ blocks alerts inside onunload):

window.onunload = function() {
myfun();
alert('Bye.');
}

Or with jQuery:

$(window).unload(function(){
myfun();
alert('Bye.');
});

Trying to detect browser close event

Have you tried this code?

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;
};

$(function () {
$("a").not('#lnkLogOut').click(function () {
window.onbeforeunload = null;
});
$(".btn").click(function () {
window.onbeforeunload = null;
});
});

The second function is optional to avoid prompting while clicking on #lnkLogOut and .btn elements.

One more thing, The custom Prompt will not work in Firefox (even in latest version also). For more details about it, please go to this thread.

Best way to intercept closing of window?

In the form you want to intercept the closing for:

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if(dontClose)
{
e.Cancel = true;
}
base.OnClosing(e);
}

Replace dontClose with your conditions for not closing.

Intercept JavaScript unload events

if you need to prevent page unload see this post:
How do I stop a page from unloading (navigating away) in JS?

If you need to track page unloads, you can make a synchronous xhr request (tip set async=false) during your window's unload event (your page will not exit until this request finishes)

if you need to track which custom event handlers unload the page, you will have to track the current event handler and then do one of the above mentioned solutions.



Related Topics



Leave a reply



Submit