Ken wrote:
Can JavaScript detect the size of an image file?
Ken
No, JavaScript can not. However, at least one user agent has a
host Image object which includes a property that lets you access
the information you require. JavaScript can then be used to
access that host object property:
<url:
http://msdn.microsoft.com/workshop/a...s/filesize.asp
/>
<body onload="alert(document.images['myImage'].fileSize);">
<img name="myImage" src="myImage.gif">
</body>
Note that of IE 6SP1, Firefox 0.9.3, Mozilla 1.7.2, Opera 7.54,
Opera 6.05 and Netscape 4.78, only IE does not report "undefined"
in the above code example.
The other possibility would be to use the XML HTTP Request object
available in IE and Gecko-based browsers to do a HEAD request for
the image. The headers that get returned should contain the image
size (Content-Length) <url:
http://jibbering.com/2002/4/httprequest.html />.
Lastly, if you have any sort of server-side technology available
to you (ASP, JSP, Perl, PHP, etc), you could use it to retrieve
the size of the image and then "pass" that value to the
client-side javascript
:
<?php
# use PHP to determine the size of the image
?>
<script type="text/javascript">
var imageSize = <?php echo $imageSize ?>;
</script>
--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq