Results 1 to 8 of 8

Thread: (javascript) getting a div's height

  1. #1

    Thread Starter
    Frenzied Member msimmons's Avatar
    Join Date
    Jul 2001
    Location
    Houston, TX
    Posts
    1,057

    (javascript) getting a div's height

    I have a div that will have a dynamic height. It will only vary on page load but will be different from time to time so I cannot hard code the height (need to know the height in a different function). Here is what I tried:
    VB Code:
    1. var temptest = getElementById('content').style.height;
    2. alert(temptest);
    this gives me the 'object expected' error...

    any hints?
    Michael
    I'm off to GalahTech, hope to see you there.

    If you don't like the rules they make, refuse to play their game. -- Steve Ignorant.

  2. #2
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    style.width only sets it, not gets the value.

    Heres the good old findxxxx functions.

    example for <div id="layer1" style="position:absolute; left:100px;">
    Code:
    alert(findleft('layer1'));
    this will (should) return 100.

    Code:
    function findleft(objectID){
      if (findDom(objectID).left){return findDom(objectID).left;}
      if (findDom(objectID).pixelLeft){return findDom(objectID).pixelLeft;}
      if (findDom(objectID).offsetLeft){return findDom(objectID).offsetLeft;}
      return 'null';
    }
    
    function findtop(objectID){
      if (findDom(objectID).top){return findDom(objectID).top;}
      if (findDom(objectID).pixelTop){return findDom(objectID).pixelTop;}
      if (findDom(objectID).offsetTop){return findDom(objectID).offsetTop;}
      return 'null';
    }
    
    function findwidth(objectID){
      if (findDom(objectID).offsetWidth){return findDom(objectID).offsetWidth;}
      if (findDom(objectID).clip.width){return findDom(objectID).clip.width;}
      return 'null';
    }
    
    function findheight(objectID){
      if (findDom(objectID).offsetHeight){return findDom(objectID).offsetHeight;}
      if (findDom(objectID).clip.height){return findDom(objectID).clip.height;}
      return 'null';
    }
    
    function findDom(objectID){
       if (document.all){
         return document.all(objectID);
       } else if (document.getElementById){
         return document.getElementById(objectID);
       } else if (document.layers){
         return (document.layers[objectID]);
       }
    }
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  3. #3
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Thanks Lee, that's one snippet for the templates directory!

  4. #4
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Now if someone wanted to rewrite that in concise, compliant JavaScript. I think all of that would drop to twelve lines.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  5. #5
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Be my guest, Travis.

    I only have it like that so I can snip the code as I need it. I use a lot of javascripts that way
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  6. #6

  7. #7
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Nothing is wrong with it. But it is 27 lines instead of 12.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  8. #8
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    So how can you compact it more?

Posting Permissions

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



Click Here to Expand Forum to Full Width