Read: http://www.quirksmode.org/js/cookies.html
And: http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS
First, lets make an example html layout.
<html> <!--#include javascript.js styles.css-->
<body>
<span>sometext</span>
<p>somemoretext</p>
</body>
</html>
Next, lets make an example css layout.
span {
color: red;
}
p {
color: blue;
}
body {
background: black;
}
Now is the javascript. Before we do anything, cookies cannot store css information. We will use cookies names to determine the type of style to load. The quirksmode resource above lets us easily use cookies.
if(readCookie("newStyle")) {
// do something
} else {
// do something else
}
In this case, we want to change the styles. We can use two methods: document.element.style.property
or appendChild
with the href of another CSS sheet. In this example, we use document.element.style.property
.
if(readCookie("newStyle")) {
eraseCookie("newStyle")
document.getElementsByTagName("span")[0].style.color = "purple"
document.getElementsByTagName("p")[0].style.color = "pink"
} else {
createCookie("newStyle",0000,60)
}
Live Example: http://jsfiddle.net/5HGsH/3/
Refresh the page multiple times to see different colored words.