如何在浏览器关闭之前通过ajax发布数据

时间:2014-09-29 09:51:27

标签: javascript jquery ajax html5

我在html unload事件上调用了一个javascript函数,但它只适用于google chrome而不是firefox和IE。

<script>
    function fun()
    {
        $.post("test.php",{name:'name'},function(data){alert(data);})
    }
    </script>

    <body onUnload="fun()">
    </body>**emphasized text**

1 个答案:

答案 0 :(得分:1)

也许你想使用onbeforeunload。 Safari,IExplorer,Firefox,Opera和Chrome都支持。

一个基本的例子:

<html>
    <head>
        <script type="text/javascript">
            window.onbeforeunload = OnBeforeUnLoad;
            function OnBeforeUnLoad () {
                return "bye world!";
            }
        </script>
    </head>
    <body>
        <b>Close or reload the page.</b>
    </body>
</html>