Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

resizing screen resolution using javascript

Status
Not open for further replies.

geniedon

Programmer
Dec 24, 2003
11
GB
hi,
i have a problem i have been given some code using javascript to resize the screen to what resolution
the user wants.

<script language="JavaScript">
if (window.screen.width >= 1024) {
URL = "index1.html";
}
else if (window.screen.width >= 800) {
URL = "index.html";
}
else if (window.screen.width >= 640) {
URL = "index2.html";
}
location.href = URL;
</script>

but the problem is want happens when i first open the website if its 1024 resolution will it go index1.html
or will it go to index.html , i would'nt want it to go there if the screen resolution was at 1024 as i think it would show index.html

can anyone hellllp.

thanks
geniedon
index.html will it go to the
 
It won't go to index.html because you're using else if instead of just another if. You can think of it this way, as the code means the same thing:
Code:
<script language="JavaScript">
if (window.screen.width >= 1024) {
  URL = "index1.html";
}
else {
  if (window.screen.width >= 800) {
    URL = "index.html";
  }
  else {
    if (window.screen.width >= 640) {
      URL = "index2.html";
    } 
  }
}
location.href = URL; 
</script>
As you can see in this example, if the first condition is true, the rest of the conditions won't even be considered. If the width of the screen is 1024 the else won't even be evaluated. Therefore, the check to see if it's greater than or equal to 800 won't be evaluted. The code I wrote is really just another way to write the code you have already supplied.

So, to answer your question, your code should work fine.

-kaht

banghead.gif
 
You can remove one resolution test:

Code:
<script language="JavaScript">
URL = "index.html";

if (window.screen.width >= 1024) {
  URL = "index1.html";
}
 else {
    if (window.screen.width >= 640) {
      URL = "index2.html";
    }
}
location.href = URL;
</script>

The assumption is that the window is going to open in 1 of 3 resolutions. So, if it's not 1024 or 640, then it has to be 800 - and there's no need to test for it.

There's always a better way. The fun is trying to find it!
 
tviman,

in your code, if the resolution is 800, when it hits the condition:
if (window.screen.width >= 640)
it will evaluate true and the URL will never be set to "index.html"

-kaht

banghead.gif
 
In the above example if the screen resolution is 800x600 then the first condition fails - 800 isn't greater than 1024. It performs another check: is 800 greater than 640? True, so the "index2.html" page is loaded... which is incorrect.

It should be:
Code:
<script language="JavaScript">
[b]URL = "index2.html";[/b]

if (window.screen.width >= 1024) {
  URL = "index1.html";
}
 else {
    if (window.screen.width >= [b]800[/b]) {
      URL = "[b]index.html[/b]";
    }
}
location.href = URL;
</script>
That is, if the resolution isn't greater than 1024, and it isn't greater than 800, then it must be 640 in width. [thumbsup2]

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
thanks kaht and tviman,
i'll get on with it and tell ya what happens

see ya
 
also thanks to WartookMan,
sorry nearly forgot bro thanks for the info will tell ya the results.

thanks
geniedon
 
Sorry fellas'

I was doing a kwik cut-n-paste and didn't look at the equality symbols. The point I was making was that you don't have to test for all 3 resolutions because we know that at one of them is going to be used.

There's always a better way. The fun is trying to find it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top