SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    USA
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cycling through style sheets?

    I'm trying to modify this script so that, vs. having individual links for each of the four style sheets I want to use, I can use one href that will cycle through all of them as you keep clicking. Please help!

    ----
    function setActiveStyleSheet(title) {
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
    a.disabled = true;
    if(a.getAttribute("title") == title) a.disabled = false;
    }
    }
    }

    function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
    }

    function getPreferredStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
    && a.getAttribute("rel").indexOf("alt") == -1
    && a.getAttribute("title")
    ) return a.getAttribute("title");
    }
    return null;
    }

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    window.onload = function(e) {
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    }

    window.onunload = function(e) {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
    }

    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    ----

    Currently, it will only work using a separate link for each sheet:

    <a href="#"
    onclick="setActiveStyleSheet('style01');
    return false;">change style to style01</a>

    <a href="#"
    onclick="setActiveStyleSheet('style02');
    return false;">change style to style02</a> ...etc.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Holy sheet...
    Code:
    <script type="text/javascript" language="javascript">
    
    var sheets = ['style01' , 'style02' , 'style03' , 'style04'];
    sheets.which = sheets.length;
    .............
    .............
    <a href="#"
    onclick="setActiveStyleSheet(sheets[sheets.which++%sheets.length]);
    return false;">change style</a>
    ::: certified wild guess :::

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    USA
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    Holy sheet...
    Code:
    <script type="text/javascript" language="javascript">
    
    var sheets = ['style01' , 'style02' , 'style03' , 'style04'];
    sheets.which = sheets.length;
    .............
    .............
    <a href="#"
    onclick="setActiveStyleSheet(sheets[sheets.which++%sheets.length]);
    return false;">change style</a>
    where does the var go? Also, do I need to add the paths above?

    this... ['style01' , 'style02' , 'style03' , 'style04'];

    to... ['styles/style01.css' , 'styles/style02.css' , 'styles/style03.css' , 'styles/style04.css'];

    Do all the style sheets have to be in the same directory?

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sheets is an array holding the titles - not file paths - of the individual stylesheets linked to your page. Stick it in the <head>er, along with the following line. All the stylesheets are downloaded with the page; the code simply disables all but one, according to user choice, previous choice (via a cookie), or your designation of a 'preferred' sheet. How you store the files on the server is unrelated to the script; wasn't there any documentation with it? All you asked for was a link that would cycle through the options....
    ::: certified wild guess :::

  5. #5
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From what I can see, this is the 'documentation':
    http://www.alistapart.com/articles/alternate/

    And Ashley, put your code in [ code] //code goes here[ /code] tags please (Ommit spaces). Makes life much easier for all of us.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    USA
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you everyone! I have it working now. I found that script on another forum but the thread seemed dormant...thus the reason for posting here. Had I known the documentation existed at A List Apart, I could've saved a lot of time. Nonetheless, thanks again!

    Ashley


Bookmarks

Posting Permissions

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