Hi all, I (beginner) would like to show the original size of a picture. I tried that: HEAD: <script type="text/javascript"> pic1 = new Image(); pic1.src = "img/C ... jpg" var breite1 = document.pic1.width var hoehe1 = document.pic1.height </script> BODY: <script type="text/javascript"> document.write(breite1) document.write(hoehe1) </script> What it shows is: "undefined" Where is the error ??
It's undefined because the image has not actually loaded yet in the above code. Try this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> <title>Test</title> <script type="text/javascript"> function loadImage() { pic1 = new Image(); pic1.onload = function() { var w = pic1.width; var h = pic1.height; document.getElementById("img_width").innerHTML += w + " px"; document.getElementById("img_height").innerHTML += h + " px"; }; pic1.src = "images/my_image.jpg"; // ... add image URL here } </script> </head> <body> <div> <p id="img_width">Image width is: </p> <p id="img_height">Image height is: </p> <input type="button" onclick="loadImage()" value="Show Image Width"> </div> </body> </html> Code (markup):
do this HEAD: <script type="text/javascript"> pic1 = new Image(); pic1.src = "img/C ... jpg"; var breite1 = document.pic1.width; var hoehe1 = document.pic1.height; </script> BODY: <script type="text/javascript"> document.write(breite1); document.write(hoehe1); </script> you forgot semicolons. common beginner mistake. or just do <script language="javascript"> document.write("<img src"source"/ >); </script> if you must use javascipt.