www.webdeveloper.com
Results 1 to 6 of 6

Thread: Dynamically switching stylesheets

  1. #1
    Join Date
    Nov 2002
    Posts
    4

    Question Dynamically switching stylesheets

    Is there a way to switch between style sheets on a page in response to a user-generated event?

  2. #2
    Join Date
    Nov 2002
    Location
    England
    Posts
    275
    Something like this?

    Code:
    <link href="style.css" type="text/css" rel="stylesheet" id="stylesheet" />
    
    <script type="text/javascript"><!--
      function changeSS(ssURI) {
        if (document.getElementById)
          document.getElementById('stylesheet').href= ssURI;
      }
    //--></script>

  3. #3
    Join Date
    Nov 2002
    Location
    England
    Posts
    275
    Yeah it does work, at least for more modern browsers (excl. Opera 6 not sure about v 7 Beta). I also have some code on my site which will change the stylesheet, and set a cookie to remember it when you change pages (if you don't have a server-side method to do it, which is the preferable way).

  4. #4
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    I played with it - you can preview the site templates for my newly established business here (still under construction): www.klproductions.com/TemplateT4.html
    While I target only DOM compliant browsers with this functionality, the site will be viewable with any other browser (even if in text mode). Still, I found the more reliable way is to set the new stylesheet in a cookie and then reload the page. IE does not not really like setting the stylesheet file dynamically (in my experience). If you manage to make it work, I would like to see the code .

    Regards

  5. #5
    Join Date
    Nov 2002
    Location
    Nashua, NH
    Posts
    3,195
    document.write
    I found the dynamic creation of style element to be unreliable in IE

  6. #6
    Join Date
    Nov 2002
    Location
    Hampshire, England
    Posts
    42
    Theres a short tutorial here that might help:

    http://www.alistapart.com/stories/alternate/

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