I am trying to get some javascript to programmatically adjust the width of the html img tag to display images of various sizes.
I have a fixed img tag of 800 pixels to display an image, this is the maximum width.
If the image is wider, then 800 pixels, I want to display it with a width of 800 pixels,
If the image size is less than 800 pixels, I want to keep its width so as not to stretch it.
I use this html / javacript code to get a partial solution:
<html> <head> <script type="text/javascript"> <!-- function resize_image( id ) { var img = document.getElementById( id ); var normal_width = img.width; img.removeAttribute( "width" ); var real_width = img.width; if( real_width < normal_width ) img.width = real_width; else img.width = normal_width; } </script> </head> <body> <img id="myimage" onload="resize_image(self.id);" src="IMAGE.JPG" width="800" /> </body> </html>
The above code works in all browsers that I tested, except for Safari (images do not appear if you do not refresh the page).
I know that I can use the maximum CSS width, but this will not work on IE <7, which is an exponential stopper.
How can I make this work for all browsers? Thank you very much in advance.
source share