简体   繁体   English

Javascript检测所有图像(包括异步)

[英]Javascript Detect All Images (Including Asynchronous)

Is there a way in javascript to detect all images in a document, including those that may be loaded asynchronously (and maybe after the DOM is ready)? javascript中是否有一种方法可以检测文档中的所有图像,包括可能异步加载的图像(可能在DOM准备就绪之后)?

I'm looking to create a function that can detect if Google Analytics has been loaded by searching through the DOM looking for "__utm.gif". 我正在寻找一个可以通过在DOM中搜索“ __utm.gif”来检测是否已加载Google Analytics(分析)的函数。 document.images doesn't seem to hold this image as it's loaded asynchronously and not displayed. document.images似乎不包含此图像,因为它是异步加载的并且不会显示。

Is there any reason you don't want to implement GA detection by checking for the HTTP request to which this gif (ie, __utm.gif) is appended? 您是否有任何理由不想通过检查此gif(即__utm.gif)附加到的HTTP请求来实现GA检测?

It's this request that's responsible for sending all of the data GA collects to the GA servers, and of course the request is made every time the page is requested or an event is fired on that page. 正是此请求负责将GA收集的所有数据发送到GA服务器,当然,每次请求该页面或在该页面上触发事件时都会发出该请求。

Again, this request has a very distinct signature--it always begins like this: 同样,此请求具有非常独特的签名-它始终像这样开始:

http://www.google-analytics.com/__utm.gif?utmwv=4&utmn http://www.google-analytics.com/__utm.gif?utmwv=4&utmn

Here's a complete example from the GA Site. 这是GA网站上的完整示例

如果您想检测GA,则只需测试window._gat和window._gaq。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM