I've got a code that works to change style sheets and saving them in cookies in Firefox and IE but it doesn't work in Chrome.
This is the code I'm using:
<head>
<link href="default.css" title="default" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<ul id="dropdown">
<li> Choose theme
<ul>
<li id="stylesheet1" > <a href="#"> Default </a></li>
<li id="stylesheet2" > <a href="#"> Theme 1 </a></li>
<li id="stylesheet3" > <a href="#"> Theme 2 </a></li>
<li id="stylesheet4" > <a href="#"> Theme 3 </a></li>
</ul>
</li>
</ul>
</body>
And the javascript.js contains this:
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);
function initate()
{
document.getElementById("stylesheet1").onclick = function() {
setActiveStyleSheet("default");
return false
};
document.getElementById("stylesheet2").onclick = function() {
setActiveStyleSheet("theme1");
return false
};
document.getElementById("stylesheet3").onclick = function() {
setActiveStyleSheet("theme2");
return false
};
document.getElementById("stylesheet4").onclick = function() {
setActiveStyleSheet("theme3");
return false
}
}
window.onload = initate;
It does work to change the style sheet in Chrome but when I reload the page it doesn't keep the chosen style sheet like it does in IE and Firefox. I can't figure why it doesn't work in Chrome. I'm new to javascript and most of the code used is not written by me but taken from different places so I don't really understand most of it.