I rewrote your code in simple JavaScript to make it more jQuery independent. The core functionality has not changed. Fiddle: http://jsfiddle.net/EmjQG/2/
function cached(url){ var test = document.createElement("img"); test.src = url; return test.complete || test.width+test.height > 0; } var base_url = "http://www.google.com/images/srpr/nav_logo80.png" alert("Expected: true or false\n" + cached(base_url) + "\n\nExpected: false (cache-busting enabled)\n" + cached(base_url + "?" + new Date().getTime()));
The first time I get false and false
. After I run the code again, I get true and false
.
Using .complete
and .height
+ .width
gives the expected results (FF 3.6.23, Chromium 14).Most likely, you have disabled caching in the Chrome browser. If not, check the HTTP headers of your displayed image (is Cache-control
present?). This title exists in the Google sample.
If you want to detect when the image has finished loading, check out this question .
source share