SunQuest
 
       Style Sheets
  Home arrow Style Sheets arrow Page 6 - Customizing styles: User-Controlled Style ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Customizing styles: User-Controlled Style Sheets – Part III
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2005-04-20

    Table of Contents:
  • Customizing styles: User-Controlled Style Sheets – Part III
  • Breaking the "changeStyle()" function into pieces
  • Listing the complete source code
  • Making the big change: multiple style sheets and persistence
  • Defining the HTML markup and Style Sheets
  • Putting the pieces together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    IBM developerWorks
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Customizing styles: User-Controlled Style Sheets – Part III - Putting the pieces together


    (Page 6 of 6 )

     

    As I promised before, here’s the full code for changing style sheets, and keeping the change persistent:

    <html>

    <head>

    <title>CHANGING STYLES BY STYLE SHEET</title>

    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1" />

    <link rel="stylesheet" type="text/css"
    href="default.css" id="default" />

    <link rel="alternate" type="text/css"
    href="style1.css" id="style1" />

    <link rel="alternate" type="text/css"
    href="style2.css" id="style2" />

    <link rel="alternate" type="text/css"
    href="style3.css" id="style3" />

    <link rel="alternate" type="text/css"
    href="style4.css" id="style4" />

    <script language="javascript">

    function setStyleSheet(sheetId){

         // loop over style sheets and deactivate them

         for(var i=0;i<document.styleSheets.length;i++){

              document.styleSheets[i].disabled=true;

         }

         // activate selected style sheet

         var sheet=document.getElementById(sheetId);

         if(!sheet){return;}

         sheet.disabled=false;

         // store stylesheet ID value in cookie

         document.cookie="style="+sheetId;

    }

    function makeStyleSwitchers(){

         // locate <a> elements in 'stylepanel' element

         var as=document.getElementById
    ('stylepanel').getElementsByTagName('a');

         if(!as){return;}

         // assign onclick event handlers

         for(var i=0;i<as.length;i++){

              as[i].onclick=function(){

                    // set style sheet

                    setStyleSheet(this.id.replace
    (/switcher/,'style'));

              }

         }

    }

    // load default style sheet if no cookie is found

    document.cookie!=''?setStyleSheet(document.cookie.split
    ('=')[1]):setStyleSheet('default');

    // make style switchers

    window.onload=function(){

         if(document.getElementById){

              makeStyleSwitchers();

         }

    }

    </script>

    </head>

    <body>

    <h1>Page Name</h1>

    <div id="stylepanel">

    <p>

    <a href="#" title="Change Style" id="switcher1">Style
    1</a>

    <a href="#" title="Change Style" id="switcher2">Style
    2</a>

    <a href="#" title="Change Style" id="switcher3">Style
    3</a>

    <a href="#" title="Change Style" id="switcher4">Style
    4</a>

    </p>

    </div>

    <div class="content">

    <p>Content for div1 goes here...Content for div1 goes
    here...Content for div1 goes here...</p>

    </div>

    <div class="content">

    <p>Content for div 2 goes here...Content for div2 goes
    here...Content for div2 goes here...</p>

    </div>

    <div class="content">

    <p>Content for div3 goes here...Content for div3 goes
    here...Content for div3 goes here...</p>

    </div>

    </body>

    </html>

    Whew, that was a pretty long snippet. But look at what we have accomplished: we’ve developed a useful method for changing complete style sheets, and we made it stick across different pages!

    Conclusion 

    In this series, we’ve taken an in-depth look at different methods to change styles in Web pages, starting out with single-element style manipulation to multiple wider style sheet changes, using only client-side techniques. Hopefully this has been an introductory process that will encourage you to expand on the explained methods and incorporate possible improvements to them. Just put your JavaScript background to work and make the big change!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Is adding all the stylesheets as rel="stylesheet". All but one should be...
       · Yes, you're correct about the usage of the "alternate" attribute. Thanks for...
       · We added the changes to the article.
       · Thanks so much for the correction in the article.Best Regards
     

    STYLE SHEETS ARTICLES

    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text
    - CSS: Backgrounds
    - CSS for the Newbie


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway