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

Thread: Javascript Window size detector

  1. #1
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Question Javascript Window size detector

    Hi,
    I have a site that is basicly "fixed" as in that the content will not move if you resize the window. I made the total width 800, so that most 800*600 browsers can read it without having to scroll(that much, I mean scrollbars won't take up that much room ) Ok, every thing works. I recently got my document valid Strict, and decided to add a browser-stactic menu, that floats(NOTE: this is NOT the same script as on DD) in a div or table, displaying the W3's Valid document icon. This adds more width to the document. I decided that if the browser size (not resoultion) was more than (about) 830 px, then I would add the extra table around the outside so that the menu could float. if it was less than 830, It would display static below the copyright. Here is the nummed-down version of the script:
    HTML Code:
    <html>
    <body>
    
    <script language="JavaScript"><!--
    function window_r()
    {
    w = 640;
    h = 480;
    NS4Plus = (document.layers) ? 1 : 0;
    IE4Plus = (document.all) ? 1 : 0;
    
    if (NS4Plus) {
       w = window.innerWidth;
       h  = window.innerHeight;
    }
    if (IE4Plus) {
    /* The script must be inside the body tags - there is no body in the head of the document */
       w = document.body.clientWidth;
       h = document.body.clientHeight;
    }
    }
    //--></script>
    <script language="JavaScript"><!--
    window_r();
    if(w>830)
    {
    document.write('<table bgcolor=red><tr><td>This was imported by javascript, and is in a red table');
    }
    </script>
    <table bgcolor=green><tr><td>
    This Text Is Green, and is not affected when a red table surounds it.
    </td></tr></table>
    <script language="JavaScript"><!--
    window_r();
    if(w>830)
    {
    document.write('</td></tr></table>This was imported by javascript, but is not in a table.<br>');
    }
    </script>
    This Text should not be affected whatever the size of the window.
    </body>
    </html>
    NOTE: I made the document so it was easy to tell which was inputed by what.
    When I added the above script into my document, only half the page showed up the top part dissappeared. I think beacause that is was not calling the function window_r(); in the seaperate <script> tags. I have no Idea what is going on, it works flaulessly on my local machine. HELP!
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  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

    That's not valid strict.
    - John
    ________________________

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

  3. #3
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I told you, that was the example, my real site is valid 4.01 Strict .
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  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

    Then link us to the real site so we can see what the problem is.
    - John
    ________________________

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

  5. #5
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Ok... here is the site link:
    http://www.klein-onlineblog.com/test.../ohp/index.php
    and here is what it looked liked BEFORE I added the extra table:
    http://www.klein-onlineblog.com/test...ohp/index2.php
    ----READ BEFORE YOU VISIT:-----
    It is written in php, but it is not the problem, I did "Save As" and it still comes up localy, so it is a javascript problem. If you wish to see the php source, just ask, I will only disply parts for copyright reasons. The site is still in devopmental stages, none of the links work on the side bar or anyware. ONLY FOCUS ON THE PROBLEM I will report you, so don't say that I spelled some stuff worng(which I know I did) or tell me that you saw that same picture on google. All of the prices\plans are not finished. PLUS all images\Ideas\Slogans are copyrighted by Onion House productions (my company).
    Last edited by fileserverdirect; 08-16-2007 at 07:28 PM.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  6. #6
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Just a quick note, your HTML validates with W3C but your CSS doesn't.
    If you submit your page here http://jigsaw.w3.org/css-validator/ it has numerous errors.

  7. #7
    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

    The page needs to be loaded before IE and other browsers that can use them can tell the clientWidth and clientHeight. This test for NS4Plus:

    Code:
    NS4Plus = (document.layers) ? 1 : 0;
    will be false for all modern browsers, so this:

    Code:
    if (NS4Plus) {
       w = window.innerWidth;
       h  = window.innerHeight;
    }
    will never get used, and this:

    Code:
    if (IE4Plus) {
       w = document.body.clientWidth;
       h = document.body.clientHeight;
    }
    will be meaningless until after the page has loaded, at which point it will be too late to do all your document.write thingers. My advice is to just scrap the entire idea and add the extra content in the normal flow of the page, somewhere that it will look OK regardless of window size.
    - John
    ________________________

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

  8. #8
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I thought of 3 possable work-arounds:

    1)I will add the main part of the script right before the </BODY> tag, so the page pretty much loaded. Then add it into a variable within a function, then call the function in another script. Is there any way for it to change the document after it loaded? shouldn't document.write do it, because it is client-side?

    2)This ones more of a question: What if I add an include function in the php to add the script to the document, even before the page loads. this way it will have an answer to "w" before the page is displayed.

    3a)Have an "entry" page that calulates the width of the document, then passes it along to the next page throgh a "get" method and then it will decide to show the table or not.

    3b) The same as above but except throwing the width variable over, it would be a simple index.php?s_valid=0 or 1 for showing it. and I could write it in php this time, even before the page loads, it would decide to print the table tags.
    ----
    I personaly like option 3b the most, due to that it could be universal throghout the site, and would only have to add 3-4 lines of $_GET and if commands (in php).
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  9. #9
    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

    I can think of a number of workarounds, and some of what you have listed might work out too. However, it is rather pointless to go to so much trouble just to show a single image. I'd just put it in the normal flow of the page, or better still, redesign the page so it can flow into whatever size window the user has open for it.
    - John
    ________________________

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

  10. #10
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Ok, I think 3b is the easiest (for me). But it would be kina hard to redesign the site because of the images set to exact pixles, but I will test it out with the entry page. Instead of having a newpage, I will just add an if command over the whole page to tell if the variable is set or not.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

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
  •