Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Getting H and W

  1. #1
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default Getting H and W

    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!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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!

  3. #3
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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

  4. #4
    Join Date
    Aug 2007
    Location
    Ohio
    Posts
    79
    Thanks
    0
    Thanked 15 Times in 15 Posts

    Default

    Quote Originally Posted by Nile View Post
    Could you explain what this does:
    Code:
    var m = new Image();
    m.src = "/blah/foo.png";
    m.onload = function() {
    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:

    Code:
    var img = document.getElementById("myImage");
    var h = img.height;
    var w = img.width;
    Your image would look like:
    Code:
    <img src="foo/bar.png" id="myImage" />

  5. #5
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    With this code:
    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>
    I get this:
    Code:
    [object HTMLImageElement]
    Last edited by Nile; 01-27-2008 at 10:52 PM.
    Jeremy | jfein.net

  6. #6
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    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.

  7. #7
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    So I should put it in the body?
    Jeremy | jfein.net

  8. #8
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    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.

  9. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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

  10. #10
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •