简体   繁体   中英

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)?

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". document.images doesn't seem to hold this image as it's loaded asynchronously and not displayed.

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?

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.

Again, this request has a very distinct signature--it always begins like this:

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

Here's a complete example from the GA Site.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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