pcx99 wrote:
Ari Krupnik wrote:
>Image.width returns the width of an image as it is displayed on a
browser page, which may be different from the image's intrinsic size
if the 'img' element had a 'width' attribute specified. Is there a
reliable way to find an image's intrinsic dimensions, irrespective of
the size specified in HTML?
<img src="Someimage.jpg" ID='testImg' width=75 height=75>
Invalid, the required `alt' attribute value is missing.
<script language='javascript'>
That is invalid, previous-century stuff. Until further notice, use this
instead:
<script type="text/javascript">
Please apply
http://validator.w3.org/ on your markup before you post.
document.writeln(document.getElementById('testImg' ).width+'<br>');
document.writeln() may not be fully supported. Use document.write("...\n")
instead, if required. Also note that calling document.write...() after the
document was loaded is highly likely to overwrite the document's content.
// returns 75 (the forced width of the IMG tag)
var test = new Image;
Although not required, it is good style to use the Call Operator on
NewExpressions:
var test = new Image();
test.src = 'Someimage.jpg';
document.writeln(test.width); // returns real width of the picture.
Apart from the document.writeln() issue mentioned above, as followups have
proven this is not a reliable way of obtaining the image size. Because,
although ECMAScript implemetations so far are single-threaded, the API call
for retrieving the image data that is triggered by assigning a value to
`test.src' is independent of script execution. Sometimes `0' (or any other
value different from the truth) can and will be written, because the image
was not loaded yet.
Be aware that you'll need to test if the image is actually loaded before
testing width or you may get unpredictable results. This worked fine
when I tested it, but the image was in my browser's cache so do some
testing about how the various browsers return width when the image
hasn't fully (or started) loading yet.
There is a well-supported, yet proprietary, event handler for this:
test.onload = function()
{
window.alert(test.width);
};
test.src = "...";
PointedEars
--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one.
-- Benjamin Franklin