bg-colorを変更する必要がある場合はlocalStorage
、ページがリロードされる前に bg が何であったかを確認するために使用できます。
var colours = ['#F00','#0F0'];//my eyes!
var currentColour = +(localStorage.previousBGColour || -1)+1;
currentColour = currentColour >= colours.length ? 0 : currentColour;//if index is not set reset to colour at index 0
document.getElementById('theDiv').style.backgroundColor = colours[currentColour];
localStorage.previousBGColour = currentColour;//store colour that's currently in use
すべてのブラウザーが をサポートしているわけではないことに注意してくださいlocalStorage
。たとえば、古くて使いにくい IE8 をまだ使用している人もいます。
jQuery
$(document).ready(function()
{
(function()
{//this IIFE is optional, but is just a lot tidier (no vars cluttering the rest of the script)
var colours = ['#F00','#0F0'],
currentColour = +(localStorage.previousBGColour || -1) + 1;
$('#theDiv').css({backgroundColor:colours[currentColour]});
localStorage.previousBGColour = currentColour;
}());
}