View Full Version : Screen resolution detection and Redirect
websiteguy
06-25-2007, 07:23 AM
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
charlesgan
06-25-2007, 09:38 AM
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.
Pethens
06-25-2007, 10:32 AM
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:
<script language="Javascript"><!--
if (screen.width <= 1023) {
document.location = "index8.html";
}
//--></script>
lazynitwit
06-25-2007, 10:58 AM
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.
felgall
06-25-2007, 01:07 PM
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.
websiteguy
06-25-2007, 02:39 PM
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
kaskudoo
06-26-2007, 08:04 PM
you should build your website content, so that it fits regardless of the browser page. a liquid layout might just do the trick :)
AfroJoJo
06-27-2007, 10:14 PM
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.
felgall
06-28-2007, 01:03 PM
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.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.