...

View Full Version : If...Else problem



pardicity3
07-23-2002, 01:10 AM
Okay, so in my <head> section of my page, I have a script that, based on what browser the user is using, chooses a specific stylesheet to use for that browser. I will give you the code first:

<script language="javascript" type="test/javascript">
<!--
if (navigator.appName=="Netscape")
{
document.write("<link rel='stylesheet' href='styles/illogical.css' type='text/css'>")
}
else
{
document.write("<link rel='stylesheet' href='styles/logical.css' type='text/css'>")
}
//-->
</script>

Now this script works perfectly in NS6. But when I am using IE It doesn't work. I really think that I am missing something incredibley stupid like a spelling mistake but I have checked over this code a million times!! I really don't see where the mistake is coming from. Hopefully you all can find a solution for me.

It's so wierd too because I have written scripts much more complex then this one and I usually always find the mistakes I make. This thing has got me rattled bad!!

Oakendin
07-23-2002, 01:26 AM
I've usually used this:
var lsBrowser = navigator.appName;
if (lsBrowser.indexOf("Microsoft") >=0) {
alert("IE!");
}

pardicity3
07-23-2002, 01:37 AM
Oak-

Your post gave me and idea...Why not try switching it to look and see if it is ie instead of looking for netscape? So I changed my code to so:

<script language="javascript" type="test/javascript">
<!--
if (navigator.appName.indexOf("Microsoft")!=-1)
{
document.write("<link rel='stylesheet' href='styles/logical.css' type='text/css'>")
}
else
{
document.write("<link rel='stylesheet' href='styles/illogical.css' type='text/css'>")
}
//-->
</script>

And guess what? It works for Netscape but not IE. This really rattles me because I thought that maybe there was a problem with the "else" part of my script, but there obviously isn't. I still thought that maybe there was a spelling error, so I changed the name of the stylesheet for ie to something different, and it didin't work either.

So rattled, don't know what to do.....It would prbably be faster to learn php and use that :D . I'm so vexed right now...

Oakendin
07-23-2002, 01:45 AM
This one should work:

<html><head><title>Untitled</title>
<script language="JavaScript1.2">
var lsBrowser = navigator.appName;
function checkBrowser(){
if (lsBrowser.indexOf("Microsoft") >=0)
alert("IE");
else
alert("NOT IE");
}
</script>
</head><body onload="checkBrowser();"></body></html>

pardicity3
07-23-2002, 01:52 AM
The strange thing is that the code you are giving me, oak, works. But when I try and modify it to write the correct stylesheet it still doesn't work. I get the same story: works in NS but not in IE. So upsetting.....

Oakendin
07-23-2002, 02:10 AM
This seems to work for me in both IE6 and Netscape4.7

<script language="JavaScript1.2">
if (navigator.appName.indexOf("Microsoft") >=0)
document.write("<link href='ie.css' rel='stylesheet' type='text/css'>");
else
document.write("<link href='ns.css' rel='stylesheet' type='text/css'>");
</script>

boywonder
07-23-2002, 02:25 AM
pardicity3, there is a syntax error in both of your examples:
<script language="javascript" type="test/javascript">

Whenever I make an error in the 'type' attribute, my scripts don't work at all. Nuts me up too because it's the last place I look. Hope that helps you.


BTW - object detection IMO is the way to go, for example...

if(document.all)
or
if(typeof document.all != "undefined")

both amount to - if browser is IE

You could write your conditional Style sheet link like this:
document.write("<link rel='stylesheet' href='styles/"+(document.all?"logical.css":"illogical.css")+"' type='text/css'>")

There's a tutorial on object detection at the old forum I believe. good luck.

pardicity3
07-23-2002, 06:34 AM
JAAAAAHHHHH (That's me getting upset)!!! That was it, all I had to do was change test (how'd I do that one??) to text. Wow, for a second there I thought I was going crazy...

Anyway, I will have to check out and see if I can find any object detection tutorials. I am really sort of new to javascript though I do understand the basics. Oh well, thanks a lot boywonder!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum