#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    6
    Rep Power
    0

    Exclamation Platform sniffer and CSS


    I am looking for some JavaScript that can determine what type of computer a person is browsing from and dynamically assign a stylesheet based on that. After doing a search, I found some code and tested it but I can't make it work! The original example used " alert('This is a Mac') " which works fine, but when I tried to switch it with some " document.write " the page goes blank after loading. My test code is below. Any help would be GREATLY appreciated!! :-) Thanks.

    -Andy

    <html>
    <head>
    <script language="JavaScript">
    <!--
    function sniffer() {
    var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
    if(isMac) {
    document.write('<link rel="stylesheet" href="mac.ccs" type="text/css">')
    }
    else {
    document.write('<link rel="stylesheet" href="pc.ccs" type="text/css">')
    }
    }
    //-->
    </script>
    </head>

    <body onLoad="sniffer()" class="bg">
    <p class="text">This is a test</p>
    </body>
    </html>

    FYI, the "mac.css" contains the following:

    .bg { BACKGROUND-COLOR: #eedd82; }
    .text { FONT-FAMILY: Verdana, Helvetica, Arial, sans-serif; FONT-SIZE: 36px ; font-weight: bold; }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    6
    Rep Power
    0

    Talking OOPs


    Sorry, just noticed that I misspelled ".ccs" After correcting it to ".css" it still doesn't work...
  4. #3
  5. T-Shirt Tragic
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    886
    Rep Power
    321
    you're calling the function in onload which is too late to write a link to a style sheet.. try it like this:-
    Code:
    <html> 
    <head> 
    <script language="JavaScript"> 
    <!-- 
    var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false; 
    if(isMac) { 
        document.write('<link href="mac.css" rel="stylesheet" type="text/css">') 
    } 
    else { 
        document.write('<link rel="stylesheet" href="pc.css" type="text/css">') 
    }
    // --> 
    </script> 
    </head> 
    <body> 
    <p>This is a test</p> 
    </body> 
    </html>
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    San Francisco
    Posts
    6
    Rep Power
    0

    Thumbs up


    Many thanks Jonathon!

    -Andy

IMN logo majestic logo threadwatch logo seochat tools logo