Style Sheets
  Home arrow Style Sheets arrow Page 4 - Using Persistent Styles with Multiple Styl...
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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Using Persistent Styles with Multiple Style Sheets
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-09-12

    Table of Contents:
  • Using Persistent Styles with Multiple Style Sheets
  • Refreshing some previous concepts and methodologies
  • Working with persistent style sheets
  • Defining some basic JavaScript functions
  • Listing the full source code of the style switching application

  • 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
     
     
    ADVERTISEMENT


    Using Persistent Styles with Multiple Style Sheets - Defining some basic JavaScript functions


    (Page 4 of 5 )

    As I stated in the section that you just read, in order to combine the styles defined by the different persistent  sheets that you saw previously, it's necessary to create some simple JavaScript functions. These functions will be tasked with assigning specific classes to the DIVs and paragraphs included into the sample web page that was built earlier.

    Based on this premise, I'm going to define these functions in such a way that they allow the user to change individually the look and feel of the mentioned elements via a set of web page links created with the DOM.

    So first, here's the signature of the JavaScript function that displays the mentioned links:

    function displayStyleLinks(){
      var labels=new Array('Red','Green','Blue','Font Size 1','Font
    Size 2','Font Size3','Reset Styles');
      var div=document.createElement('div');
      div.setAttribute('id','linkcontainer');
      for(var i=0;i<labels.length;i++){
        var lnk=document.createElement('a');
        lnk.setAttribute('href','#');
        lnk.setAttribute('title',labels[i]);
        lnk.appendChild(document.createTextNode(labels[i]));
        div.appendChild(lnk);
      }
      document.getElementsByTagName('body')[0].appendChild(div);
    }

    As you can see, the prior "displayStyleLinks()" function is responsible for displaying the list of links that will change the styles of the different DIVs and paragraphs included in the sample web page defined previously, so I believe the logic implemented by it doesn't bear much discussion in this case.

    Therefore, let's turn our attention to the signatures of the following functions, which are tasked with assigning different classes to the DIVs and paragraphs contained in the same sample web document built earlier.

    These brand new functions look like this: 

    function changeDivStyle(style){
      var divs=document.getElementsByTagName('div');
      if(!divs){return};
      for(var i=0;i<divs.length;i++){
        if(divs[i].className.indexOf('container')!=-1){
          divs[i].className='container '+style;
        }
      }
    }

    function changeParStyle(style){
      var divs=document.getElementsByTagName('div');
      if(!divs){return};
      for(var i=0;i<divs.length;i++){
        if(divs[i].className.indexOf('container')!=-1){
          var pars=document.getElementsByTagName('p');
          if(!pars){return};
          for(var i=0;i<pars.length;i++){
            pars[i].className=style;
          }           
        }
      }
    }

    In basic terms, the primary task of all the above JavaScript functions is to assign different CSS classes to the respective web page DIVs and paragraphs. However, things get really interesting when I include these classes into different persistent style sheets, as illustrated below:

    (definition for "red.css" file)

    .red{
      background: #f00;
    }

    (definition for "green.css" file)

    .green{
      background: #0f0;
    }

    (definition for "blue.css" file)

    .blue{
      background: #00f;
    }

    (definition for "fontsize1.css" file)

    .size1{
      font: normal 14pt Arial, Helvetica, sans-serif;
    }

    (definition for "fontsize2.css" file)

    .size2{
      font: normal 18pt Arial, Helvetica, sans-serif;
    }

    (definition for "fontsize3.css" file)

    .size3{
      font: normal 24pt Arial, Helvetica, sans-serif;
    }

    Now, having defined the previous persistent style sheets, it's perfectly possible to apply them to the different paragraphs and DIVs of the previous sample web page, in this way achieving a neat visual combination.

    How can this be done? Well, the answer is actually simple. By associating the proper JavaScript function to a specific styling link, it's feasible to style DIVs and paragraphs individually, which is demonstrated by the following piece of JavaScript code:

    window.onload=function(){
      if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
        // display style links on the browser
        displayStyleLinks();
        var link1=document.getElementsByTagName('a')[0];
        if(!link1){return};
        link1.onclick=function(){
          changeDivStyle('red');
          return false;
        }
        var link2=document.getElementsByTagName('a')[1];
        if(!link2){return};
        link2.onclick=function(){
          changeDivStyle('green');
          return false;
        }
        var link3=document.getElementsByTagName('a')[2];
        if(!link3){return};
        link3.onclick=function(){
          changeDivStyle('blue');
          return false;
        }
        var link4=document.getElementsByTagName('a')[3];
        if(!link4){return};
        link4.onclick=function(){
          changeParStyle('size1');
          return false;
        }
        var link5=document.getElementsByTagName('a')[4];
        if(!link5){return};
        link5.onclick=function(){
          changeParStyle('size2');
          return false;
        }
        var link6=document.getElementsByTagName('a')[5];
        if(!link6){return};
        link6.onclick=function(){
          changeParStyle('size3');
          return false;
        }
        var link7=document.getElementsByTagName('a')[6];
        if(!link7){return};
        link7.onclick=function(){
          changeDivStyle('');
          changeParStyle('');
          return false;
        }
      }
    }

    Now, each time one of the previous web page links is clicked on, different style combinations can be achieved easily for the same web document. Of course, you'll grasp this process better if you look at the following images, which show this combination of styles in action:

    See how easy it is to combine the styles defined in different persistent sheets by using a few basic JavaScript functions? I bet you do! Now that you hopefully grasped the logic that drives the approach for utilizing multiple persistent style sheets, it's time to leap forward and list the complete source code that corresponds to this style switching application, so you can see more clearly how its distinct pieces link with each other.

    This will be done in the last section of this tutorial, so click on the link that appears below and keep reading.

    More Style Sheets Articles
    More By Alejandro Gervasio


       · This second article of the series introduces you to the use of multiple persistent...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek