
Originally Posted by
ramtanion
the reason I need to set width/height limits in the javascript is because they are hyperlinked from image upload sites and I don't have control over the originals.
any ideas on that?
Well, it is still a bad idea. If there is any way to get the servers on those upload sites to resize the images or to refuse images that are beyond the allowed dimensions, that would be the way to go. Either of these are possible but are beyond the realm of both this script and javascript in general. However, it does require sufficient access to the servers. You might not have that.
If it must be done by the browser though, virtually all current version (this includes IE 7 but not IE 6) browsers will allow for this type of styling (goes in the head before the script call):
Code:
<style type="text/css">
#loadarea img {
max-width:600px;
max-height:350px;
}
</style>
where loadarea is the id of the element that will hold the larger image. This id is also used as the rev attribute of the link in this script or as the first part of the rev attribute, if the image is linked.
Now, I did some testing locally and this worked out for IE 6 (goes right after the above style):
Code:
<!--[if IE]>
<script type="text/javascript">
function ie6getdims(img, mw, mh){
var w=img.width, h=img.height, rw='auto', rh='auto';
if(w>mw){
rw=mw+'px';
rh='auto';
img.style.width=rw;
img.style.height=rh;
h=img.height;
}
if(h>mh){
rw='auto';
rh=mh+'px';
}
img.style.width=rw;
img.style.height=rh;
}
</script>
<style type="text/css">
* html #loadarea img {
width:expression(ie6getdims(this, 600, 350));
}
</style>
<![endif]-->
However, it depends upon the browser knowing the actual height and width of the image. This is virtually instantaneous with local images, but IE in general, and IE 6 is no exception, is one of the best browsers at getting this information from an image early in the image loading process. Add to that the fact that using the expression technique in style will usually cause the operation to be repeated over and over, and there is a good chance that this will also work out well live. If it doesn't, don't use it - you can still set the width for IE 6 only:
Code:
<style type="text/css">
* html #loadarea img {
width:600px;
}
</style>
In the above example, all images will then be 600px wide and their height will scale proportionally to that.
Added later: I have now actually tested this IE 6 method with a remotely hosted image, and it worked fine.
Bookmarks