Results 1 to 6 of 6

Thread: Scrollbar detection

  1. #1
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scrollbar detection

    I need a script that will detect if there are scrollbars, and if there is, then insert <style type="text/css">body { height:auto; }</style> into the header.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by MarioGamer99 View Post
    I need a script that will . . .
    I really doubt that (there must be a better way of dealing with your design problem(s), and directly assigning the style to the body would probably do just as well, and cause less backward compatibility issues). However, this will come about as close as is possible to doing that in modern browsers (IE will not accept it, but will apply the style directly to the body element). A DOCTYPE that triggers standards compliance is required:

    Code:
    onload=function(){
    var portH=window.innerHeight||document.documentElement.clientHeight;
    if(document.documentElement.scrollHeight>portH){
    try {
    var s=document.createElement('style');
    s.type="text/css";
    s.appendChild(document.createTextNode('body {height:auto;}'))
    document.getElementsByTagName('head')[0].appendChild(s);
    }catch(e){
    document.body.style.height='auto';
    }
    }
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    THANKS!
    It seems to work fine in IE6 and 7. It's perfect. I've spent days trying to get my layout to work with pure CSS, and your script makes it work!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In that case, you should probably just use:

    Code:
    onload=function(){
    var portH=window.innerHeight||document.documentElement.clientHeight;
    if(document.documentElement.scrollHeight>portH)
    document.body.style.height='auto';
    }
    as that is all that it is doing in IE, and other browsers can do it that way as well.

    I still think there must be a better way to get your design to work though.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I hope there is, but I've been trying a lot. Maybe weeks. I've only had a makeshift solution by manually making the page longer than the screen with line breaks.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If body {height:auto;} is so nice when the page doesn't fit vertically (I am assuming that's the story), what are you using for the body height otherwise? Generally the body height should simply not be defined, then it will default to auto or something like that. That way, if your page fits the window, fine. Otherwise you will have a vertical scrollbar. It's that simple.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •