javascript - Writing and Retrieving Cookies in JS -
alright have assignment class , i'm not entirely sure i'm doing wrong. need insert initpage() function sbutton calls savemailinginfo() , has run when page loads. need write savemailinginfo() function creates 3 year expiration date , loops through each element of mailing form. did right don't alert when test it. ideas?
/* function attachs event handler under both event models */ function addevent(object, evname, fnname, cap) { if (object.attachevent) object.attachevent("on" + evname, fnname); else if (object.addeventlistener) object.addeventlistener(evname, fnname, cap); } function writecookie(cname, cvalue, expdate, cpath, cdomain, csecure) { if (cname && cvalue != "") { var cstring = cname + "=" + escape(cvalue); if (expdate) cstring += ";expires=" + expdate.togmtstring(); if (cpath) cstring += ";path=" + cpath; if (cdomain) cstring += ";domain=" + cdomain; if (csecure) cstring += ";secure"; document.cookie = cstring; } function savemailinginfo() { var expire = new date(); expire.setfullyear(expire.getfullyear() + 3); var allfields = document.mailingform.elements; (var = 0; < allfields.length; i++) { if (allfields[i].type == "text") { writecookie(allfields[i].id, allfields[i].value, expire); } if (allfields[i].nodename == "select") { writecookie(allfields[i].id, allfields[i].selectedindex, expire); } if (allfields[i].type == "radio") || allfields[i].type == "checkbox") { writecookie(allfields[i].id, allfields[i].checked, expire); } } } alert("cookie has been written!"); } addevent(window, "load", initpage, false); function initpage(){ document.getelementbyid("sbutton").onclick = savemailinginfo; console.log(document.cookie); } <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- dw6 --> <head> <!-- --> <title>cakes emily</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="main.css" type="text/css" /> <script type="text/javascript" src="cakeform.js"></script> </head> <body > <div class="head" id="header"> <p><img src="cake.jpg" alt="cake picture" /><img src="logo.gif" alt="cakes emily" width="400" height="125" /></p> <div class="links" id="navigation"> <a href="#">home</a> <a href="#">cakes</a> <a href="#">pastries</a> <a href="#">pies</a> <a href="#">mailing list</a> </div> </div> <div class="maincontent"> <p>enter info added our email list reminders getting birthday cake want! remember @ cakes emily, aim make every birthday best!</p> <form name="mailingform" > <table> <tr> <td> first: </td> <td> <input type="text" name="firstname" id="firstname"><br> </td> </tr> <tr> <td> last: </td> <td><input type="text" name="lastname" id="lastname" ><br> </td> </tr> <tr> <td> e-mail: </td> <td><input type="text" name="email" id="email" ><br> </td> </tr> <tr> <td> birthday (mm/dd/yyyy): </td> <td><input type="text" name="birthday" id="birthday" ><br> </td> </tr> <tr> <td> favorite cake: </td> <td> <select id="favoritecake"> <option value="buttercream">butter cream</option> <option value="chocolate">chocolate</option> <option value="vanilla">vanilla</option> <option value="redvelvet">red velvet</option> </select><br> </td> </tr> <tr> <td> frequency of emails: </td> <td><input type="radio" name="frequency" id="frequency1" >email me monthly<br> <input type="radio" name="frequency" id="frequency2" >email me quarterly<br> <input type="radio" name="frequency" id="frequency3" >email me near birthday<br> </td> </tr> </table> <input type="submit" id="sbutton" name="sbutton" value="join our mailing list" /> <input type="reset" value="reset form" /> </form> </div> <div class="footer"> </div> </body> </html>
Comments
Post a Comment