By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

File Size

P: n/a
Can JavaScript detect the size of an image file?


Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
On Mon, 6 Sep 2004 15:39:18 -0500, Ken wrote:
Can JavaScript detect the size of an image file?

I do not know if there are other ways to do it, but
...does your server return file listings like this?

That is a directory on my server that contains
no 'index' page, so the server writes a file list
...complete with file size.

Assuming you can access the elements using JS,
you might be able to load that page in a hidden
or IFRAME and parse it for the information.

Be warned though, that I am merely lurking here
trying to pick up tips from the experts. ;-)

Andrew Thompson Open-source software suite Web & IT Help Science & Technology
Jul 23 '05 #2

P: n/a
Ken wrote:
Can JavaScript detect the size of an image file?


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:


<body onload="alert(document.images['myImage'].fileSize);">
<img name="myImage" src="myImage.gif">

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: />.

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:

# use PHP to determine the size of the image
<script type="text/javascript">
var imageSize = <?php echo $imageSize ?>;

Grant Wagner <gw*****>
comp.lang.javascript FAQ -

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.