JS & CSS Style Changer

IamShipon1988

New Member
Messages
942
Reaction score
0
Points
0
This is a very cool script that alloys you to make a very attracting and customizable template for your website. This is called a style changer. It allows visitors to change the layout according to a selected stylesheet. The following continues after you have your main and the 4 changeable stylesheets already created for the template.

Step 1:

Create a file called stylechanger.js
Step 2:
Publish the following code within the newly created file
Code:
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);
Step 3:
Before your header tag add the following code
Code:
<style type="text/css">@import"style.css";</style>
<link rel="stylesheet" href="style1.css" type="text/css" title="blue">
<link rel="alternate stylesheet" type="text/css" href="style2.css" title="red">
<link rel="alternate stylesheet" type="text/css" href="style3.css" title="green">
<link rel="alternate stylesheet" type="text/css" href="style4.css" title="pink">
&lt;script type="text/javascript" src="styleswitcher.js"></script>
(Please modify the "style1.css, style2.css, etc. to the name of your stylesheets, you can change the title of the script but I recommend you leave it as it is)
Step 4:
Where you want the button to appear, add the following code
Code:
<a href="#" onclick="setActiveStyleSheet('blue'); return false;" title="Change background to nature" ><img src="images/tbimg2.jpg" alt="Change background to sharp grass" border="0" /></a> 
<a href="#" onclick="setActiveStyleSheet('red'); return false;" title="Change style to Red"><img src="images/tbimg1.jpg" alt="Change style to Red" border="0" /></a> 
<a href="#" onclick="setActiveStyleSheet('green'); return false;" title="Change style to Green"><img src="images/tbimg3.jpg" alt="Change background to cat-eye" border="0" /></a> 
<a href="#" onclick="setActiveStyleSheet('pink'); return false;" title="Change background to sky"><img src="images/tbimg4.jpg" alt="Change style to Pink" border="0" /></a>
(Please note that you have to change images/tbimg1.jpg (2,3,4) with the location of your image. You can also modify the alternate hover title to what ever you wish. Remember what I said before, not to change the name, well this is why, now you will have to change the names again here and make sure you call the right stylesheet)
Step 5:
Now save and test to see if it works.

If you would like to view a stylechanger script that is coded in php, please click here

COPYRIGHT 2007+ SAZZAD HOSSAIN
 
Top