Results 1 to 9 of 9

Thread: Screen resolution detection and Redirect

  1. #1
    Join Date
    Jun 2007
    Location
    Oshkosh, Wisconsin
    Posts
    2

    Default Screen resolution detection and Redirect

    Hi,
    Hopefully someone can see my error right away. I'm new at javascript, but do understand programming. I installed this script immediately after the <head> tag on my index page.

    <script language="Javascript"><!--

    if (screen.width <= 1023) {
    document.location = "index8.html";
    }

    if (screen.width > 1024) {
    document.location = "index.html";
    }

    //--></script>

    At 800 x 600, it loaded the smaller version as directed. At 1024 x 768 it loaded the larger version as directed. At 1152 and larger, it just repeatedly clicks and goes nowhere in IE7.0. Firefox also seems to be stuck in a loop.

    I've tried adding an if else statement and a couple of other changes, but no luck. Can anyone help?
    Thanks,
    Curt

  2. #2
    Join Date
    Mar 2007
    Location
    http://www.bluehostreview.org
    Posts
    1,133

    Default

    previously i test some simple javascript at IE and firefox.
    seem like they not work the same way. firefox just dont work.

    probably you need 2 set of javascript, one for ie, another for firefox.
    Charles Gan
    www.bluehostreview.org : Bluehost reviews hosted at bluehost:cool:

  3. #3
    Join Date
    Oct 2006
    Posts
    358

    Default

    To me it looks like your second if statement is creating an infinite loop-- you are already in the file index.html when this code runs, right?

    So take it out:

    Code:
    <script language="Javascript"><!--
    
    if (screen.width <= 1023) {
    document.location = "index8.html";
    }
    
    //--></script>

  4. #4
    Join Date
    Feb 2006
    Location
    Florida, USA
    Posts
    153

    Default

    Quote Originally Posted by websiteguy View Post
    Hi,
    Hopefully someone can see my error right away. I'm new at javascript, but do understand programming. I installed this script immediately after the <head> tag on my index page.

    <script language="Javascript"><!--

    if (screen.width <= 1023) {
    document.location = "index8.html";
    }

    if (screen.width > 1024) {
    document.location = "index.html";
    }

    //--></script>

    At 800 x 600, it loaded the smaller version as directed. At 1024 x 768 it loaded the larger version as directed. At 1152 and larger, it just repeatedly clicks and goes nowhere in IE7.0. Firefox also seems to be stuck in a loop.

    I've tried adding an if else statement and a couple of other changes, but no luck. Can anyone help?
    Thanks,
    Curt
    The reason 1024x768 doesn't have this problem is because no conditional matches for a width of 1024.

    Remember, > is greater than and >= is greater than or equal to. So using the logic defined by your code; if the screen resolution is less than or equal to 1023, it gets redirected to index8.html. If the resolution is greater than 1024 it gets redirected to index.html (you should really use a different file, as this is causing the loop). Since 1024 is not within either conditional, nothing happens for screens with a resolution of 1024x768 and you just simply stay on index.html.
    BlueHost Knowledge Base, it is quite helpful, you should read through it.

  5. #5
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,944

    Default

    Why are you testing the screen size when the page has to display in the browser window which may be much smaller. A screen width of 2000+ with a window width of 200 is quite possible since browser windows do not have to be opened full screen. At larger screen resolutions the browser is less likely to be open full screen.

  6. #6
    Join Date
    Jun 2007
    Location
    Oshkosh, Wisconsin
    Posts
    2

    Default Screen resolution detection and Redirect

    Hi,
    Problem solved!!
    Thanks Pethens! I used your rewrite and it's working perfectly. I should have seen that, but I'm always learning.

    You and lazynitwit were right on target.

    Thanks to both of you,
    Curt

  7. #7

    Default

    you should build your website content, so that it fits regardless of the browser page. a liquid layout might just do the trick :)

  8. #8
    Join Date
    Dec 2006
    Location
    Wisconsin
    Posts
    152

    Default

    Quote Originally Posted by kaskudoo View Post
    you should build your website content, so that it fits regardless of the browser page. a liquid layout might just do the trick :)
    I agree with that.

  9. #9
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,944

    Default

    If you absolutely MUST have multiple layouts then select based on browser window size and not screen size as screen size is completely irrelevant to the space in the browser.

Posting Permissions

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