Hey.
I wanted to know how you could get a height and width of an image with JS, I know how to do it with PHP tho, so any help appreciated!
![]()
Hey.
I wanted to know how you could get a height and width of an image with JS, I know how to do it with PHP tho, so any help appreciated!
![]()
Code:var m = new Image(); m.src = "/blah/foo.png"; m.onload = function() { this.height; // for the height this.width; // for the width };
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Could you explain what this does:
Code:var m = new Image(); m.src = "/blah/foo.png"; m.onload = function() {
Last edited by Nile; 01-27-2008 at 09:29 PM.
Jeremy | jfein.net
The first line creates a new image element pointed to by the variable m. This image element is just like any other image element that's on a webpage, except this image isn't included in the document yet and it is a completely generic image element. At it's current state, there is no image file associated with the element. It'd be the equivalent of creating writing an image without any src attribute, like:
Code:<img />
The second line sets the src property to the image you want to check the width of or height of. You would replace '/blah/foo.png' with the path to the image you want to use.
The third line sets the 'onLoad' 'event handler' of the image to the function within the brackets. This tells the browser that once the image is loaded to execute that function. You wouldn't want to get the width and height before the image was loaded.
Also, if you wanted to find the width and height of an image already within the document, you can do that too. The simplest way would be to set an id for the image element, and use the document.getElementById() function to get the image. Here's an example:
Your image would look like:Code:var img = document.getElementById("myImage"); var h = img.height; var w = img.width;
Code:<img src="foo/bar.png" id="myImage" />
With this code:
I get this:Code:<script type="text/javascript"> var m = new Image(); m.src = "test.jpg"; m.onload = function() { this.height; // for the height this.width; // for the width };document.write(m);</script>
Code:[object HTMLImageElement]
Last edited by Nile; 01-27-2008 at 10:52 PM.
Jeremy | jfein.net
the image() function is used to preload an image, and still has to be declared in the body.
[Jasme Library (Javascript Motion Effects)] My Site
/\/\@§†ê® §©®¡þ† /\/\@|{ê®
There are 10 kinds of people in the world, those that understand binary and those that don't.
sorry, i wasn't clear. the image still has to be declared in the body
if you want to get an images height i think you should just use:Code:var img = document.getElementById("imageid"); var h = img.height; var w = img.width;
Last edited by Master_script_maker; 01-28-2008 at 12:00 AM.
[Jasme Library (Javascript Motion Effects)] My Site
/\/\@§†ê® §©®¡þ† /\/\@|{ê®
There are 10 kinds of people in the world, those that understand binary and those that don't.
I just wanna know why this script isn't workiing?:
Code:<script type="text/javascript"> var m = new Image(); m.src = "test.jpg"; m.onload = function() { this.height; // for the height this.width; // for the width }; document.write(m); </script>
Jeremy | jfein.net
it doesn't work the way you intended because you can't write an image element that way. to add it to the document you would do this(replaces the span with an image):
HTML Code:<span id="image_1"></span>Code:<script type="text/javascript"> var m = new Image(); m.src = "image_1.jpg"; m.onload = function() { var h=this.height; // for the height var w=this.width; // for the width }; var span=document.getElementById("image_1"); var body=document.getElementsByTagName("body"); body[0].insertBefore(m, span); body[0].removeChild(span); </script>
Last edited by Master_script_maker; 01-28-2008 at 12:57 AM.
[Jasme Library (Javascript Motion Effects)] My Site
/\/\@§†ê® §©®¡þ† /\/\@|{ê®
There are 10 kinds of people in the world, those that understand binary and those that don't.
Bookmarks