I am facing a challenge with dynamically changing and resizing an image element to fit its container.
My current approach involves:
Resetting the image:
// Ensuring the 'load' event is re-triggered img.src = ""; // Resetting dimensions img.style.width = "auto"; img.style.height = "auto";
Setting the new image source and waiting for it to load
img.src = newImageSource;
In the image's
onload
handler, checking and adjusting its size if necessary:img.style.width = newWidth + "px";
This process repeats every time the image changes (infinite loop).
Although this method works well on most browsers tested (IE7, 8, 9, 10, FF, Chrome), IE6 seems to resize the element to around 25 x 25 px when width/height are set to "auto," regardless of the actual image dimensions.
Therefore, I am looking for a way to reset the image's dimensions to mimic "auto" so that IE6 can adjust the elements based on the loaded image's dimensions. Is there a solution for this?