在卸载/ beforeunload上使用Javascript发送帖子请求。那可能吗? [英] Sending a post Request with Javascript on unload/beforeunload. Is that possible?

查看:905
本文介绍了在卸载/ beforeunload上使用Javascript发送帖子请求。那可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

补充:我不能使用jQuery。我使用的是西门子S7控制单元,它有一个小的网络服务器,甚至无法处理80kB的jQuery文件,因此我只能使用原生的Javascript。从这个链接在页面卸载时使用JQuery的Ajax请求我得到了我需要使请求同步而不是异步。可以用原生Javascript完成吗?

Added: I cannot use jQuery. I am using a Siemens S7 control unit which has a tiny webserver that cannot even handle a 80kB jQuery file, so I can only use native Javascript. from this link Ajax request with JQuery on page unload I get that I need to make the request synchronous instead of asynchronous. Can that be done with native Javascript?

我从这里复制了代码: JavaScript发布请求,如表单提交

I copied the code from here: JavaScript post request like a form submit

我想知道是否可以在关闭窗口时调用此方法/ tab /使用jquery离开网站beforeunload或unload。应该是可能的,对吗?

I was wondering if I could call this on closing the window/tab/leaving the site with jquery beforeunload or unload. Should be possible, right?

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}


推荐答案

这是一种方法它:

<body onunload="Exit()" onbeforeunload="Exit()">
    <script type="text/javascript">
        function Exit()
        {
            Stop();
            document.body.onunload       = "";
            document.body.onbeforeunload = "";
            // Make sure it is not sent twice
        }
        function Stop()
        {
            var request = new XMLHttpRequest();
            request.open("POST","some_path",false);
            request.setRequestHeader("content-type","application/x-www-form-urlencoded");
            request.send("some_arg="+some_arg);
        }
    </script>
</body>

请注意,请求可能必须是同步的(调用 request.open with async = false )。

Note that the request probably has to be synchronous (call request.open with async=false).

另一个值得关注的重点:

如果客户端突然终止(例如,浏览器以结束进程或掉电关闭),那么 onunload 事件也不会触发 onbeforeunload ,并且不会将请求发送到服务器。

If the client is terminated abruptly (e.g., browser is closed with "End Process" or "Power Down"), then neither the onunload event nor the onbeforeunload will be triggered, and the request will not be sent to the server.

这篇关于在卸载/ beforeunload上使用Javascript发送帖子请求。那可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆