mcmuney Posted January 22, 2008 Share Posted January 22, 2008 Can anyone provide a javascript that will resize any image that exists on a page that exceeds a given number to a different size? For example, lets say that an image exists on the page which has a width of 1200. On the script, my criteria for the width is 600. In this example, since the image being displayed at 1200, which exceeds the 600 maximum, it'll display the image at 600. Thanks. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 22, 2008 Share Posted January 22, 2008 you don't have to use javascript for this; you can just use css. <style type="text/css"> IMG {width:600px} </style> <img src="whatever.jpg" width=1200 height=900> Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 22, 2008 Author Share Posted January 22, 2008 I've tried img {max-width:500px} , which works in Firefox, but it doesn't work in IE. I don't know why? Is it possible because the image being displayed does not have the width and height tags? Any suggestions for IE? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 23, 2008 Share Posted January 23, 2008 try this (but you will have to give each of your pictures a unique id - you can do this dynamically or statically): <script language="javascript"> function checkSize(wide,what) { if (wide > 600) { document.getElementById(what).style.width="600"; } } </script> <img id="pic1" src="picture1.jpg" onload="checkSize(this.width,this.id)"> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 23, 2008 Share Posted January 23, 2008 actually try this instead; this version will maintain the current height and only change the width: <script language="javascript"> function checkSize(wide,high,what) { if (wide > 600) { document.getElementById(what).style.width="600"; document.getElementById(what).style.height=""+high+""; } } </script> <img id="pic1" src="test.jpg" onload="checkSize(this.width,this.height,this.id)"> Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 23, 2008 Author Share Posted January 23, 2008 Same problem, it works on Firefox, but not on IE. You had it right the first time, the second version distorts the image. Is it just my IE or what's causing this? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 23, 2008 Share Posted January 23, 2008 its work in FFv2.0.0.4 and IE6 (I did not test in IE7, don't have it on this computer - but it should work the same way); because I tested in both. the second version should not distort the image; I made it, so it would only resize the width of the image. the first version I made resized the width with javascript and the browser was automatically resizing the height due to the width size. this was because the image (as you stated) would not have a standard width/height property. Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 23, 2008 Author Share Posted January 23, 2008 I have IE 6.0.29. Do I need a plugin? Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 23, 2008 Share Posted January 23, 2008 no; try to clear out your cache/temporary internet files and then close the browser and re-open it. then open the example I gave you and it should work fine. sounds like you are having a cache issue; it's pretty common in IE6. Quote Link to comment Share on other sites More sharing options...
mcmuney Posted January 23, 2008 Author Share Posted January 23, 2008 Bingo!!! Thanks!!! Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted January 23, 2008 Share Posted January 23, 2008 your welcome Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.