www.webdeveloper.com
Results 1 to 11 of 11

Thread: Javascript Stylesheet Switcher

Hybrid View

  1. #1
    Join Date
    May 2005
    Posts
    63

    Javascript Stylesheet Switcher

    Hey,

    I'm trying to write a stylesheet switcher. What should happen in this switcher is that, on page load, it will detect whether you are using Internet Explorer or another browser, and then it will load the appropriate stylesheet.

    This is what i've got so far:

    PHP Code:
    //Stylesheet Switcher

    if (navigator.appName Microsoft Internet Explorer) {
    document.write("styleIE.css")
    }
    else {
    document.write("style.css")

    i'm not sure how to tell it to put "style.css" or "styleIE.css" in the appropriate location, which would be in the header like so:
    Code:
    <link rel="stylesheet" type="text/css" href="*correctstylesheetname*">
    and then i'm guessing the code to execute the script would be

    Code:
    <body onload="styleswitcher.js">
    can somebody check my work and offer assistance where necessary?

    Thanks!

  2. #2
    Join Date
    Dec 2004
    Posts
    8,637
    Forget the onload. Just put the following in the HEAD section of your page:
    HTML Code:
    <script type="text/javascript">
    <!--// Stylesheet Switcher
    if (navigator.appName = "Microsoft Internet Explorer") {
        document.write('<link rel="stylesheet" href="styleIE.css" type="text/css">');
    } else {
        document.write('<link rel="stylesheet" href="style.css" type="text/css">');
    }
    //-->
    </script>
    <noscript>
      <link rel="stylesheet" href="style.css" type="text/css">
    </noscript>

  3. #3
    Join Date
    May 2005
    Posts
    63
    hey,

    thanks - with a little modification this worked.

    for future reference, this is the correct code:

    PHP Code:
    <script type="text/javascript">
    <!--
    // Stylesheet Switcher
    if ((navigator.appName).indexOf("Microsoft")!=-1) {
    document.write('<link rel="stylesheet" href="styleIE.css" type="text/css">');
    }
    else {
    document.write('<link rel="stylesheet" href="style.css" type="text/css">');
    }
    //-->
    </script>
    <noscript>
    <link rel="stylesheet" href="style.css" type="text/css">
    </noscript> 

  4. #4
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by kendokendokendo
    thanks - with a little modification this worked.
    Don't look a gift-hourse in the mouth. I wasn't trying to correct your logic. I was only solving the specific question asked ... how to use document.write in order to specify a stylesheet selection.

  5. #5
    Join Date
    Jul 2003
    Location
    Boston Area, Massachusetts
    Posts
    3,489
    You must remember though that not all users have JavaScript available, so this will fail about 1 in 10 times. Further, CSS alone can get the job done by working around the quirks presented by Microsoft's joke of a browser.

    http://centricle.com/ref/css/filters/

  6. #6
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by fredmv
    You must remember though that not all users have JavaScript available, so this will fail about 1 in 10 times.
    If you were paying attention, there were NOSCRIPT tags used to provide a default style sheet in that case.

    But, yes, a CSS-only solution would be better.

    I wouldn't call a product a joke that has the ability to command most of the browser market. It must have SOME redeeming quality that makes it the most popular.
    Last edited by phpnovice; 06-07-2005 at 04:22 PM.

  7. #7
    Join Date
    May 2005
    Posts
    63
    i'd like to think of it this way:

    1. it's bundled with windows

    2. people have no choice to get windows because:
    a) they don't want to pay for a mac
    b) they don't want to figure out linux
    c) they're stupid

    3. next to nobody reads tech/web magazines that explain why IE is a serious security hazard

    i'm probably missing a bunch of reasons tho.

  8. #8
    Join Date
    Jul 2003
    Location
    Boston Area, Massachusetts
    Posts
    3,489
    Quote Originally Posted by phpnovice
    I wouldn't call a product a joke that has the ability to command most of the browser market. It must have SOME redeeming quality that makes it the most popular.
    Um, it's installed by default on Windows? Oh yeah, that's it. It's nothing more than a security hole that can't understand HTML or JavaScript. Thankfully, Mozilla is getting more and more mainstream.

  9. #9
    Join Date
    Dec 2004
    Posts
    8,637
    It doesn't matter if it is installed by default with Windows. Any product not worth it's weight in gold will be ignored and a rival product installed for use instead. That's the nature of market competition. I'm not the biggest fan of IE, but neither Mozilla nor any other browser will ever surpass IE. People are not a stupid as you (or those like you) would make them out to be.
    Last edited by phpnovice; 06-07-2005 at 11:40 PM. Reason: grammar

  10. #10
    Join Date
    Jul 2003
    Location
    Boston Area, Massachusetts
    Posts
    3,489
    Quote Originally Posted by phpnovice
    I'm not the biggest fan of IE, but neither Mozilla nor any other browser will never surpass IE.
    Too late, Mozilla and Opera are already lightyears ahead of IE in terms of DOM (Level 2, I might add) support, CSS2 and even a few neat tricks from CSS3. When you factor in how much more secure these two browsers are, not to mention far superior usability (e.g., tabbed browsing)... you realize how false your statement really is. The only way someone could support IE is they are 1) a Microsoft fanboy, or 2) a Microsoft employee (and even that's not always true, consdering many Microsoft employees have admitted to using Firefox).

  11. #11
    Join Date
    Dec 2004
    Posts
    8,637
    Quote Originally Posted by fredmv
    Too late, Mozilla and Opera are already lightyears ahead of IE in terms of DOM (Level 2, I might add) support...
    Surely you're not really that dense? When I said "surpass" I was talking about market share. Sheesh!

    Otherwise, no, I'm not a big fan of IE -- even though I prefer the flexibility of IE over any other browser (by market share, I'm in good company there). I just can't stand those who bad-mouth things about which they don't really know what they're talking about. Where software is concerned, all of it has its share of problems. People who focus on one company's software problems are just picking on the big target because it's easy. Such people are merely hungry for the aggrandizement of fellowshipping with others who share their predilection for bottom-feeding. Eventually, maybe, such people will grow up and get a life of their own so that they don't have to vicariously share in the lives of others through common fallacious hypotheses.

    But, that's OK. You just go right on ahead. I know when I'm talking to a brick wall. Besides, we are all entitled to our own opinions. Eh?

    Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles