Search code examples
javascriptonbeforeunload

how to alter beforeunload event in javascript


Is calling an ajax function possible before someone's decide to leave the page. I mean i can ask the user if user wants to leave or stay. But i dont know how to alter behaviour when user click "yes leave the page" button.

I have this code working..

<head>
<script type="text/javascript">
    function catchExit() {
        return "All your data is going to be lost. Are you sure??";
    }
</script>
</head>
<body onbeforeunload="return catchExit()">
<p>
    Dont leave the page.
</p>
</body>

Solution

  • Actually, i found a way to get my $.ajax() call worked. Setting async parameter to false works in firefox, chrome, iexplorer and safari. It only doesn't work in opera.