3

我正在尝试在来自网页的 img src=... 调用上实现即发即弃,并且正在寻找最可靠的方法。为什么?

在大多数 Web 跟踪系统(如 Omniture 或 Coremetrics)中,都会请求一个 img src,它也恰好携带了跟踪系统要记录的所有数据。与任何其他图像请求一样,页面会尝试等待响应,这可能会减慢页面加载速度。

我正在尝试做的是实现一个跟踪系统,其中网页响应时间比请求是否实际成功具有更高的优先级,因此在所有条件下等待响应都是完全没有必要的,包括......

  1. 服务器收到请求,并可以轻松响应(HTTP 响应代码 200)
  2. 服务器收到请求,但陷入困境,仍然可以轻松响应错误 500 响应代码
  3. 服务器根本没有运行
  4. 由于 DNS 故障,找不到服务器
  5. 请求到达了服务器,但被卡住了,需要很长的时间来响应。

我已经使用 XMLHttpResponse 对象和一个异步请求进行了调查,该对象可能会悄悄失败,但您很快就会遇到同源策略和浏览器兼容性代码膨胀。我相信一个简单的 jane img 请求仍然是最好的请求机制,但我正试图用最少的代码找出最好的即发即弃的实现。到目前为止,我知道我可以...

onerror="this.parentNode.removeChild(this)" onload="this.parentNode.removeChild(this)"

This works pretty well on scenarios 1 through 4 where there is a finite and predictable conclusion to the request. It breaks down in scenario 5, which could be avoided if there were something in-between.

I'm toying with the idea of seeing if this.src.readyState == "complete" but see that I am heading down a road of browser compatiblity and race-condition hell were it would be wise to tap the StackOverflow community first.

Anyone have experience with this and a good solution?

4

6 回答 6

2

为什么不在加载后将 img 标签添加到您的文档中呢?

于 2010-01-28T16:05:22.233 回答
1

您需要异步加载它。谷歌做一个,如果你想要更重量级的东西,eVisitAnalyst 做类似的事情。

异步完成的好处是,如果 .js 无法获得,您的页面就不需要等待它。

于 2010-12-21T16:31:51.647 回答
0

This is an old question, but it starts off with misinformation and I didn't see anyone correct that. CoreMetrics and Adobe and many others do not use image requests that load on the dom, that can block the page, they use JS image objects, like, outImage = new Image(); That doesn't get attached to the DOM and doesn't block in the same way as an image tag would. The 1x1 pixel reponse is just to match up with the MIME type btw.

Any JS that is loaded to implement tags has far more impact.

于 2014-11-05T22:32:37.963 回答
0

我的 PoV:用户体验第一,跟踪第二。我使用过 WebTrends/Omniture 和 Coremetrics,是的,它们确实提供了关于请求的非常精细的细节(稍后你可以按照你想要的方式对其进行分段)。即使没有实施这些网络分析供应商,您也可以从服务器日志中获取大量信息,您只需要自己进行分段(您可以根据您的条款和条件记录 IP,而 Omniture/Coremetrics 无论如何都会这样做,页面请求,执行的操作,例如结帐,添加到购物车等)。尽管这些网络分析供应商非常了解性能,他们只是向您发送 1x1 像素作为响应,但如果他们的服务器速度很慢,您的用户体验很差(有时会发生google urchins for me,我等待这张图片加载很长时间)。<img src="vendor url"/>到html dom。即使请求失败或者需要很长时间,现在用户也不会觉得网站不可用

于 2010-01-28T16:24:15.847 回答
0

正如另一个答案所暗示的,您可以img在文档加载后简单地将标签添加到图像中。

这带来了两个子问题:1)您如何知道文档何时加载以及 2)如何添加img标签。我建议将$(document).ready()JQuery 用于 1)和一些 DOM 操作用于 2)(也与 JQuery 一起使用)。

有一些方法可以检测文档何时完全加载并插入标签,但使用框架(JQuery 以外的替代方案应该没问题,我只是建议一些我知道有效的方法)可以为您节省一些精力,尤其是在制作时事情在多个浏览器上工作。

编辑:错字

于 2010-01-28T16:28:15.667 回答
0

您可以使用针对 iframe 的表单进行 POST(相同或不同的域都可以)。

拨打电话后,您可以删除 iframe 或重新使用它。

这是一个例子:

<form target="targetIfr" action="http://domain.com/" method="post">
    <input name="data" type="text" value="hello" />
</form>
<iframe name="targetIfr"></iframe>

你可以这样称呼它:

frm.submit();
ifr.parentNode.removeChild(ifr);
于 2010-01-28T16:29:35.097 回答