Hi All,

I want to run a HIDDEN FUNCTION when I click "OK"...not "CANCEL" in window.onbeforeunload() is it possible?

<script type="text/javascript">
window.onbeforeunload = function()
{
    var bUnsavedFields = true;

    if(bUnsavedFields)
    {
        window.event.returnValue = "I WANT THE OK BUTTON NOT CANCEL :) TO TRIGGER HIDDEN FUNCTION AFTER EXIT";
    }
    setTimeout("myFunction();",5000);
}

function myFunction(){
alert ("hello");
}
</script>

Yes, window.onunload :D

I have a login system and I want the user if he exit the browser he will be automatically logout on his account.

Yes, just move it to onunload, without the timeout.

window.onbeforeunload = function()
{
    return confirm('Do you want to leave this page?'); // or somethign like that
};
 
window.onunload = function()
{
    some_logout_stuff();
    something_else();
};

But, at a second thought, don't use this. If a user has two tabs with your application, and exits one of them, he/she is logged out. Instead, let sessions handle this for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.