Results 1 to 4 of 4

Thread: Help with Browser Detection

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default Help with Browser Detection

    OK so i am trying to run a stylesheet based on the browser. Just the actual browser not the version etc.

    here is the code i am using:
    Code:
    <link id="sheet" name="sheet" rel="stylesheet" type="text/css" href="main_style.css" />
    <link rel="stylesheet" type="text/css" href="ie_style.css" />
    
    <script language="javascript">
    
    function detect()
    	var isIE = (navigator.appName == "Microsoft Internet Explorer");
    	
    	if(isIE)
    	{
          document.styleSheets[0].disabled = true;
          document.styleSheets[1].disabled = false;   
      }
      else
      {
          document.styleSheets[0].disabled = false;
          document.styleSheets[1].disabled = true;
      }
    }	
    
    </script>
    Now the else works in non-ie browsers but the if doesnt work in IE. I tried putting a document.write() in the if block and el;se block and i found that in IE it is running the if statement and not the else statement but it is not working. I am really confused about whats wrong. anyone who could help would be awesome. Thanks in advance!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If that's all you want, use conditional comments:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <!--[if IE]>
    <link rel="stylesheet" href="ie.css" type="text/css">
    <![endif]-->
    <!--[if !IE]> <-->
    <link rel="stylesheet" href="all_others.css" type="text/css">
    <!--> <![endif]-->
    </head>
    <body>
    
    </body>
    </html>
    No javascript required.

    However, there is a better way. The above is mutually exclusionary. IE gets one stylesheet, all others get another. Much duplication is likely between the two stylesheets. If you do it like so:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" href="main.css" type="text/css">
    <!--[if IE]>
    <link rel="stylesheet" href="ie.css" type="text/css">
    <![endif]-->
    </head>
    <body>
    
    </body>
    </html>
    then both IE and all others will get the main stylesheet. Only IE will get the ie.css, which will only need to have things in it that are different than the main styles.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    thank you for your reply. i did not even think about using conditional comments.
    I did however get a solution to my problem before i read this post...it was to replace the square brackets [ ] in the IE section to parentheses ( ). And you are correct about rewriting a lot of the same styles, but i noticed with the code i have by disabling them it still uses the styles from both stylesheets (at least mozilla does). Thank you again for your reply and i will probably have to change it to conditional since it would probably run faster too?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The difference in run time would be so insignificant as to be negligible, but yes, comments should be faster. Browsers that don't qualify will see the part in the disqualifying area as an ordinary comment tag and breeze right through it. With branching javascript, all the code is parsed to make sure there are no errors or objects that it needs to pay attention to.

    Unless I read your javascript wrong, it should be exclusionary, unless FF doesn't understand disabled with style in the way it's used in your code.

    However, the javascript method has other pitfalls (besides requiring javascript being active on the client's browser). Since it relies upon sniffing, it can be spoofed by browsers that report themselves as IE even when they are not. No browser will follow the IE 'path' in a conditional comment unless it truly qualifies.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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