javascript - Persistent Cookie -
document.setbackground.bgcolor.value
certifiably works , stores hex value (such "000000" black. script works changing color of background of page when new 1 selected live form updates value.
my problem while code stores cookie browser, not work when close browser , reopen. want background start out whatever stored in cookie. there wrong code or browser settings?
function setbackground () { if (document.setbackground.bgcolor.value != "none"){ document.body.bgcolor = "#" + document.setbackground.bgcolor.value; document.cookie = "bgcolor=" + document.setbackground.bgcolor.value + ";expires=wednesday, 02-mar-2020 12:00:00 gmt;"; } }
this code embedded following html in setpreference.js file:
<html> <head> <script src="setpreference.js"></script> </head> <body bgcolor="#9999cc"> <center> <form name="setbackground"> change background color? <select name="bgcolor" onchange="setbackground();" size="1"> <option value="none">select color</option> <option value="9999cc">lavender</option> <option value="999966">light brown</option> <option value="66ffcc">light green</option> <option value="ffffff">white</option> </select> </center> </form> </body> </html>
when user selects item select
you'll set bgcolor value , create cookie.
but need load cookie , set bgcolor.
you can use simple (append js):
// document.cookie - name1=value1; name2=value2; name3=value3 function getcookie(what) { var cookies = document.cookie.split(/; ?/g); // have array of name=value pairs for(var in cookies) { var pair = cookies.split("=", 1); var name = decodeuricomponent(pair[0]); var value = decodeuricomponent(pair[1]); if (name == what) return value; } } window.onload = function () { var color = getcookie("bgcolor"); // note in cookie can value comes user. condition validate input wouldnt bad idea document.setbackground.bgcolor.value = color; setbackground(); }