窗口关闭之前,IMG请求窗口、IMG

2023-09-10 19:45:44 作者:旧岛空心°

我需要的浏览器窗口关闭之前被可靠地发送数据的情况。我目前的实现是使用同步AJAX调用。但是这是不太可能在不久的将来的工作,因为浏览器去precating根据的 https://xhr.spec.whatwg.org/#synchronous-flag

I have the situation that data needs to be reliably sent before browser window closes. My current implementation is to use a synchronous AJAX calls. However that's unlikely to work in the near future because the browsers are deprecating synchronous XHR calls according to https://xhr.spec.whatwg.org/#synchronous-flag

我想要是换上一个假的IMG调用Ajax调用,参数要发送的数据并将其追加为图像的URL查询字符串。它似乎到目前为止我试过的工作。我真的不关心服务器的响应,这样,只要提出要求,并被推到了电线的浏览器窗口被卸载。

What I'm trying is to replace the ajax call with a fake "img" call, parameterize data to be sent and append it as the image's url query string. It seemed to work so far I tried. I don't really care about the server response so that as long as the request is made and pushed to the wire before browser window is unloaded.

我的问题是它是多么可靠?有没有人得到任何画家经验?

My question is how reliable it is? Has anyone gotten any expeirences?

我的其他选项是为了保持数据在cookie或webstorage并发送下一个请求,但是这是基于这样的假设用户将重新审视这可能不是在我的情况属实。

My other options is to keep the data in a cookie or webstorage and send them on the next request but that's based on the assumption that user will revisit which may not be true in my case.

感谢。

推荐答案

我想更好地利用Ajax请求。我没有证据,但是从我的经验性,DOM工作速度很慢,JS。举个例子,当你做这样一句:

I think better use ajax request. I have no proof, but from my expirience, dom work slowly then js. For an example, when you do this one:

var div = document.createElement('div');
div.innerHTML = "mama";
div.className = "myDiv";
document.getElementById("myWrapper").appendChild(div);
var text = document.getElementByClassName('myDiv')[0].innerHTML;

有时你会得到异常的消息 - 无法读取未定义的innerHTML属性。 但是,如果你能做到这一点。

sometimes you will get exception with message - can't read property innerHTML of undefined. But, if you will do that

    setTimeout(function(){
        var text = document.getElementByClassName('myDiv')[0].innerHTML;
    }, 50);

它的工作永诺的罚款。这是因为DOM还没有更新。所以,在添加图像,DOM可能无法对其进行处理。而且,当你发送Ajax请求,它会sended在我认为,任何情况下。

it work allways fine. It's because dom still not updated. So, when you add image, dom may not be able to process it. And, when you send ajax request, it will be sended in any case I think.

 
精彩推荐
图片推荐