Search code examples
javascriptonbeforeunload

Can I prevent onbeforeunload from being called when clicking on an anchor tag?


In head:

<head>
    <script type="text/javascript">                          
        $(document).ready(function() {
            $("#anchor_id").click(clickHandler);
            window.onbeforeunload = confirmExit;
        });

        function clickHandler() {
            /* hide/show some hidden div's */
            $("body").append("<p>Hi there</p>");
        }

        function confirmExit() {
            return "There are some unsaved changes on page.";
        }
    </script>
</head>

In body:

<a id="anchor_id" href="javascript: void(0);">Click Me</a>

Is it possible to prevent onbeforeunload from being called when clicking on this link in Internet Explorer? In the rest of the browsers the code is working fine.


Solution

  • Try setting href="#" instead, and return false in the clickHandler function (or prevent the default event by event.preventDefault())