We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Locked Topic
JavaScript; Little Prob
Topic Started: May 18 2005, 10:16 AM (381 Views)
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
I will point out here I know no javascript; I'm trying to blag it.

OK, on my site, I want javascript to detect the monitor resolution, and depending on it, take the user somewhere else.

I currently have:

Code:
 
<script language="text/javascript">
function detect(){
if(screen.width==800&&screen.height==600)
{
 self.location="index1.php";
}

else if (screen.width=1024&&screen.height==768)
{
 self.location="index2.php";
}

else if (screen.height==1200&&screen.height==1024)
{
 self.location="index3.php";
}

else
{
 self.location="index.php";
}
}
</script>


Any help would be greatly appreciated.
Offline Profile Goto Top
 
Das
Member Avatar
Smells of rich mahogany
[ *  *  *  *  *  *  * ]
Correct me if I am wrong, but you can only have one else statement, and it is window.location
Offline Profile Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
OK, if that is the case, can someone write me a switch...case statement, or whatever I'd need to use, please?
Offline Profile Goto Top
 
Rory
i;m a mess
[ *  *  *  *  *  *  * ]
your if() {
} else if {
} else if {
} else {
}

should be fine, i know it is with php, not sure about the syntax of JavaScript though.
Offline Profile Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
It's fine in JavaScript.

It's the same syntax as in PHP. But you can't use colons like in PHP.
This is JS ifelseifelse :
Code:
 
if (condition) {
   conditional stuff
} else if (condition) {
   conditional stuff
} else {
   conditional stuff
}

If your conditional stuff for a condition is one line, you can leave out the curly braces enclosing it.
Offline Profile Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
OK, so now I have:

Code:
 
<script language="text/javascript">
function detect(){
if(screen.width==800||screen.height==600)
{
 window.location="index1.php"
}

else if (screen.width==1024||screen.height==768)
{
 window.location="index2.php"
}

else if (screen.height==1200||screen.height==1024)
{
 window.location="index3.php"
}

else
{
 window.location="index.php"
}
}
</script>


And it still doesn't work.

My mate said to try putting this in the body:
Code:
 
<script language="text/javascript">
detect();
</script>

So I've tried with and without it, but still no luck. Any more help?
Offline Profile Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
Quote:
 
<script language="text/javascript">


If you use
Quote:
 
<script type="text/javascript">

and include detect() (a function has to be called, otherwise the code isn't run), it should work.

The way you have it now, browsers will detect "text/javascript" as being the script language. That script language doesn't exist, so it won't be run.
type="text/javascript" gives the script the "text/javascript" MIME type, which means as much as "text that should be handled as javascript". And modern browsers know how to handle that.
Offline Profile Goto Top
 
JoeC
Euch! IE tastes horrible!
[ *  *  *  *  * ]
It works :eh: Thanks alot, that's great Stefan :)

Edit: Oh, and can you lock / delete this now please? Just clutter :)
Offline Profile Goto Top
 
Stefan
Member Avatar
Mew?
[ *  *  *  *  *  *  * ]
As you wish ^_^
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Locked Topic