I am having trouble with changing the src of an image in Chrome (other Webkit based browsers seem to be okay).
In the javascript I am pre-loading the new image, and then changing the src of the existing image to that of the new image. But Chrome doesn’t actually update the display until the onload function of the new image has completed (it seems).
The problem with this is that it means you can’t run any code that relies on the image’s src having been updated (e.g. getting the offsetWidth of the new image).
This example shows the problem: http://www.iliveinabin.com/Chrome-ad-blocker-test/ (links when clicked should change the image to a 500px transparent GIF and then alert “image should now be switched”).
I found that setting the image src to a blank string before changing to the new image’s src fixes the problem in Chrome when the AdBlock plugin is disabled. Using setTimeout to trigger the code following the src being changed seems to work when the AdBlock plugin is enabled as well.
Anyone else come across this or have any info?
The code for the test page is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Chrome Ad blocker test</title>
</head>
<body>
<img src="./01.png" alt="Pogs > Icee 01.png">
<p><a href="#" onclick="return s(false)">Switch Image normally</a></p>
<p><a href="#" onclick="return s(0)">Switch Image setting src to blank string first</a></p>
<p><a href="#" onclick="return s(true)">Switch Image with timeout</a></p>
<script type="text/javascript">function s(t){
var k = document.getElementsByTagName('img')[0];
var j = new Image();
j.onload = function(){
k.oldsrc = k.src;
if(t === 0){k.src = "";}
k.src = j.src;
t ? setTimeout(alert, 1, "image should now be switched") : alert("image should now be switched");
}
j.src = k.oldsrc ? k.oldsrc : "./blank.gif";
return false;
}</script></body>
</html>