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 4 of 4
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,802
    Thanks
    19
    Thanked 157 Times in 148 Posts

    simple browser detection

    This code:

    Code:
    if (navigator.appName == 'Microsoft Internet Explorer') {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }
    
    else if (navigator.appName == 'Opera') {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_opera.css">');
    }
    
    else if (navigator.appName == 'Netscape') {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos.css">');
    }
    
    else {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }
    does not seem to effectively serve the appropriate stylesheet to Opera browsers...it works for the other browser types, so why not Opera?
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Opera is difficult to detect reliably as it often spoofs Internet Explorer.

    Have a look at
    http://webreference.com/programming/...pt/sniffing/3/
    http://javascriptkit.com/javatutors/navigator.shtml

    and other pages brought up by Google.
    Hope this helps.
    Last edited by Philip M; 10-17-2005 at 07:35 PM.

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    946
    Thanks
    0
    Thanked 129 Times in 128 Posts
    Code:
    if (navigator.appName == 'Microsoft Internet Explorer') {
    if (navigator.userAgent.indexOf('Opera')==-1){
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }else{
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_Opera.css">');
    }
    }
    else if (navigator.appName == 'Netscape') {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos.css">');
    }
    else {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }
    detects Opera

    and if you want to detect Firefox too, use this code:
    Code:
    if (navigator.appName == 'Microsoft Internet Explorer') {
    if (navigator.userAgent.indexOf('Opera')==-1){
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }else{
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_Opera.css">');
    }
    }
    else if (navigator.appName == 'Netscape') {
    if (navigator.userAgent.indexOf('Firefox')==-1){
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos.css">');
    }else{
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_FF.css">');
    }
    }
    else {
    document.write('<link rel="stylesheet" type="text/css" href="index_with_css_pos_IE.css">');
    }
    the problem is that Opera's navigator.appName is "Microsoft Internet Explorer"
    as well as Firefox's navigator.appName is "Netscape"

    there it must look for additional information in navigator.userAgent
    Last edited by Lerura; 10-17-2005 at 09:27 PM.

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,802
    Thanks
    19
    Thanked 157 Times in 148 Posts
    Thanks, this helps...:-)
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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