7 replies [Last post]
Coder101
Offline
Regular
Last seen: 15 years 26 weeks ago
Joined: 2005-02-14
Posts: 36
Points: 0

I'm having this weird problem in javascript when I try to do a simple redirect. Here is my code:

if (screen.width <= 640) {
var referrer = "640x480.htm";
}

else if (screen.width <= 800) {
var referrer  = "800x600.htm";
}

else if (screen.width > 800) {
var referrer  = "larger.htm";
}
alert ("Sending you to page");
top.location.replace (referrer);

No matter what the screen width is... it will always send you to larger.html. Any help is much appreciated

I may be different... but you are all the same.

Tags:
Anonymous
Anonymous's picture
Guru

Re: Javascript Problem

if (screen.width <= 640) {
var referrer = "640x480.htm";
}

else {if (screen.width <= 800) {
var referrer  = "800x600.htm";
}

else {if (screen.width > 800) {
var referrer  = "larger.htm";
}
}
}
alert ("Sending you to page");
window.location=referrer;

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 46 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Javascript Problem

Hmm, Triumph, you seem to have two extra closing braces.

First off, have you checked what the value for screen.width is?

var referrer = "larger.htm";

if (screen.width <= 640) { referrer = "640x480.htm";}
else if (screen.width <= 800) { referrer  = "800x600.htm"; }


alert ("Screen Width:"+screen.width+"; Sending you to page "+referrer+" ...");
top.location.replace (referrer); 

Anonymous
Anonymous's picture
Guru

Javascript Problem

Chris..S wrote:
Hmm, Triumph, you seem to have two extra closing braces.
No, count them again.

The code I posted works but is messy. I could have done it cleaner, I'm sure. Smile

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 46 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Javascript Problem

Ah! I see them Laughing out loud

To be honest I see no reason why the code posted at the top shouldn't work. It will only set referrer to larger.htm if screen.width is greater than 800. Hence my point to check the screen.width value. I figure its has to be >800. Wink

Anonymous
Anonymous's picture
Guru

Javascript Problem

Chris..S wrote:
Ah! I see them Laughing out loud

To be honest I see no reason why the code posted at the top shouldn't work. It will only set referrer to larger.htm if screen.width is greater than 800. Hence my point to check the screen.width value. I figure its has to be >800. WinkYes, you are correct. Perhaps the original poster is changing the width of the browser window and not the screen resolution (which is what screen.width returns).

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 5 hours 51 min ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

browser width

Hi Coder101,
Really having three pages to maintain is not a good idea.
You would be better off looking at min-width and max-width and expressions or scripts to get that working in IE.
Here's a working method of what you are trying. It might need a little refining.

var w=0;
    if(document.compatMode == "CSS1Compat"){
        //IE Standard mode
       w =document.body.parentNode.clientWidth;
    }else if(document.currentStyle){
        //IE Quirks Mode
       w =document.body.clientWidth;
    }else{
        //Netscape
        w = window.innerWidth;
    } 
   w=parseInt(w);
   if (w <= 640) {
       var referrer = "640x480.htm";
   }else if (w<= 800) {
       var referrer  = "800x600.htm";
   }else if (w > 800) {
       var referrer  = "larger.htm";
   }
alert ("Sending you to page");
top.location.replace (referrer); 
Hope that helps

Anonymous
Anonymous's picture
Guru

Re: browser width

Tony wrote:
Hi Coder101,
Really having three pages to maintain is not a good idea.
Tony is 100% correct on this. Code forking (or in this case full-page forking[?]) is an idea of the past. I'm sure you have a perfectly good reason for it but I'm sure someone here can come up with 1,000 perfectly good reasons to abandon the idea. Smile