Closed Thread
Results 1 to 5 of 5

Thread: Style Sheet Switcher...from random to manual

  1. #1
    infopigiste is offline Newbie
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Style Sheet Switcher...from random to manual

    Hi,

    I'm using the code below to switch from a CSS to the other on my website.

    It's working fine but does anyone knows if it's possible to edit the script so that it starts with the random mode (sessiononly) to select a CSS and then, when you click a button on your website, you can switch to another CSS and it will stick on this one even if you change the page?

    This is what I was trying to do on my website: when I click a button (top right of the site) it will change the CSS but it will go back to the first one when I change the page..

    I don't know javascript as you can see..

    Anyone can help me with this??

    Thanks

    Code:
    //Style Sheet Switcher version 1.1 Oct 10th, 2006
    
    //////No need to edit beyond here//////////////
    
    function getCookie(Name) { 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return null
    }
    
    function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
    }
    
    function deleteCookie(name){
    setCookie(name, "moot")
    }
    
    
    function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
    var i, cacheobj, altsheets=[""]
    for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
    if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
    cacheobj.disabled = true
    altsheets.push(cacheobj) //store reference to alt stylesheets inside array
    if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
    cacheobj.disabled = false //enable chosen style sheet
    }
    }
    if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
    var randomnumber=Math.floor(Math.random()*altsheets.length)
    altsheets[randomnumber].disabled=false
    }
    return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
    }
    
    function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
    if (document.getElementById){
    setStylesheet(styletitle)
    setCookie("mysheet", styletitle, days)
    }
    }
    
    function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
    if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
    var element=(element.type=="select-one") ? element.options : element
    for (var i=0; i<element.length; i++){
    if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
    if (element[i].tagName=="OPTION") //if this is a select menu
    element[i].selected=true
    else //else if it's a radio button
    element[i].checked=true
    break
    }
    }
    }
    }
    
    if (manual_or_random=="manual"){ //IF MANUAL MODE
    var selectedtitle=getCookie("mysheet")
    if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored
    setStylesheet(selectedtitle)
    }
    else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE
    if (randomsetting=="eachtime")
    setStylesheet("", "random")
    else if (randomsetting=="sessiononly"){ //if "sessiononly" setting
    if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty
    document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value
    else
    setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie
    }
    else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting
    if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
    setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value
    setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
    }
    else
    setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie
    } 
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    infopigiste is offline Newbie
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Re: Style Sheet Switcher...from random to manual

    sorry, part of the code is missing in my previous post:

    Code:
    var manual_or_random="manual" //"manual" or "random"
    var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.
    
    //////No need to edit beyond here//////////////
    
    function getCookie(Name) { 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return null
    }
    
    function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
    }
    
    function deleteCookie(name){
    setCookie(name, "moot")
    }
    
    
    function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
    var i, cacheobj, altsheets=[""]
    for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
    if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
    cacheobj.disabled = true
    altsheets.push(cacheobj) //store reference to alt stylesheets inside array
    if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
    cacheobj.disabled = false //enable chosen style sheet
    }
    }
    if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
    var randomnumber=Math.floor(Math.random()*altsheets.length)
    altsheets[randomnumber].disabled=false
    }
    return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
    }
    
    function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
    if (document.getElementById){
    setStylesheet(styletitle)
    setCookie("mysheet", styletitle, days)
    }
    }
    
    function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
    if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
    var element=(element.type=="select-one") ? element.options : element
    for (var i=0; i<element.length; i++){
    if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
    if (element[i].tagName=="OPTION") //if this is a select menu
    element[i].selected=true
    else //else if it's a radio button
    element[i].checked=true
    break
    }
    }
    }
    }
    
    if (manual_or_random=="manual"){ //IF MANUAL MODE
    var selectedtitle=getCookie("mysheet")
    if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored
    setStylesheet(selectedtitle)
    }
    else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE
    if (randomsetting=="eachtime")
    setStylesheet("", "random")
    else if (randomsetting=="sessiononly"){ //if "sessiononly" setting
    if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty
    document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value
    else
    setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie
    }
    else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting
    if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
    setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value
    setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
    }
    else
    setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie
    } 
    }

  4. #3
    ArtoStiloz's Avatar
    ArtoStiloz is offline Programmer
    Join Date
    Sep 2008
    Location
    Denmark
    Posts
    173
    Rep Power
    14

    Re: Style Sheet Switcher...from random to manual

    I do not know if I understand you right, but something like this?

    Code:
    <?php
    $random = rand(1,3);
    
    if($random == 1) {
    // Include your css
    }
    elseif($random == 2) {
    // Include your css
    }
    elseif($random == 3) {
    // Include your css
    }
    ?>
    Last edited by ArtoStiloz; 09-26-2008 at 07:19 AM.

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Style Sheet Switcher...from random to manual

    Quote Originally Posted by ArtoStiloz View Post
    I do not know if I understand you right, but something like this?

    Code:
    <?php
    $random = rand(1,3);
    
    if($random == 1) {
    // Include your css
    }
    elseif($random == 2) {
    // Include your css
    }
    elseif($random == 3) {
    // Include your css
    }
    ?>
    Something like that would work, but that would load a new style each page load, not each session. I'm not sure if it is possible to have JavaScript store session data. Perhaps the best way would be to use a cookie.

  6. #5
    ArtoStiloz's Avatar
    ArtoStiloz is offline Programmer
    Join Date
    Sep 2008
    Location
    Denmark
    Posts
    173
    Rep Power
    14

    Re: Style Sheet Switcher...from random to manual

    Code:
    <head><?php
    if($_SESSION[CSS]=="") {
    $random = rand(1,3);
    
    if($random == 1) {
    $_SESSION[CSS] = "<style type=\"text/css\">@import\"random1style.css\";</style>";
    }
    elseif($random == 2) {
    $_SESSION[CSS] = "<style type=\"text/css\">@import\"random2style.css\";</style>";
    }
    elseif($random == 3) {
    $_SESSION[CSS] = "<style type=\"text/css\">@import\"random3style.css\";</style>";
    }
    }
    
    print "$_SESSION[CSS]";
    ?>
    </head>
    Last edited by ArtoStiloz; 09-26-2008 at 07:20 AM. Reason: edit

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 07-22-2011, 11:42 AM
  2. add to an excel sheet
    By lordwittie in forum Visual Basic Programming
    Replies: 5
    Last Post: 04-05-2010, 04:18 AM
  3. Byte Switcher
    By Guest in forum Classes and Code Snippets
    Replies: 0
    Last Post: 09-27-2009, 03:19 AM
  4. Undo in vi - vi cheat sheet
    By Pan in forum Linux Applications
    Replies: 1
    Last Post: 07-28-2008, 05:43 AM
  5. Display an excel sheet
    By engr in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 03-14-2007, 10:00 PM

Tags for this Thread

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