Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If...Else problem

    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!!
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.

  • #2
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've usually used this:
    var lsBrowser = navigator.appName;
    if (lsBrowser.indexOf("Microsoft") >=0) {
    alert("IE!");
    }

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 . I'm so vexed right now...
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.....
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.

  • #6
    New Coder
    Join Date
    Jul 2002
    Location
    Los Angeles
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If...Else problem

    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.
    Last edited by boywonder; 07-23-2002 at 03:02 AM.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!!!
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •