So I have an assignment I've been working on for 6 hours. I'm supposed to use JavaScript to create a cookie and display the cookie information on the user profile page of a website.
In attempting this I've written a bunch of code, and ideally I was wanting to use the submit button on the form to initially save the information as a cookie, and then display the same information when the viewer returned to the web page. Failing that repeatedly I've just put information in the cookies. So as long as it works, I don't care how it works, on submit, or with the information I plugged in.
So here's the JavaScript:
<script type="text/javascript" language="javascript">
function setCookie(cookieName, cookieValue, cookiePath, cookieExpires){
cookieValue = escape(cookieValue);
if (cookieExpires == ""){
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 6);
cookieExpires = nowDate.toGMTString();
}
if (cookiePath != ""){
cookiePath = ";Path=" + cookiePath;
}
document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + cookiePath;
}
setCookie("cookieFirstName", "George", "", "");
setCookie("cookieLastName", "MaGoo", "", "");
setCookie("cookieEmail", "[email protected]", "", "");
setCookie("cookieTelNumber", "4095555545", "", "");
setCookie("cookieBirthdate", "12/19/2004", "", "");
setCookie("cookieCity", "Westend", "", "");
setCookie("cookieState", "California", "", "");
function getCookie(cookieFirstName){
var cookieValue = document.cookie;
var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
if (cookieStartsAt == -1){
cookieStartsAt = cookieValue.indexOf(cookieName + "=");
}
if (cookieStartsAt == -1){
cookieValue = null;
} else {
cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
if (cookieEndsAt == -1){
cookieEndsAt = cookieValue.length;
}
cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
}
return cookieValue;
}
function yummyCookie(){
var cFirst = getCookie("cookieFirstName");
if (cFirst != null){
document.getElementById("firstname").value = cFirst;
}
var cLast = getCookie("cookieLastName");
if (cLast != null){
document.getElementById("lastname").value = cLast;
}
var cEmail = getCookie("cookieEmail");
if (cEmail != null){
document.getElementById("email").value = cEmail;
}
var cTelNumber = getCookie("cookieTelNumber");
if (cTelNumber != null){
document.getElementById("telnumber").value = cTelNumber;
}
var cBirthdate = getCookie("cookieBirthdate");
if (cBirthdate != null){
document.getElementById("birthdate").value = cBirthdate;
}
var cCity = getCookie("cookieCity");
if (cCity != null){
document.getElementById("city").value = cCity;
}
var cState = getCookie("cookieState");
if (cState != null){
document.getElementById("state").value = cState;
}
}
</script>
This information was placed in the tags. The body of the website looks like this:
<body onLoad="yummyCookie()">
<div class="wrapper">
<header class="center" id="banner">
<h1>Lindsey's Pet Picture Paradise</h1>
</header>
<aside id="menu" class="floatleft">
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="businessprofile.html">Business Profile</a></li>
<li><a href="userprofile.html">User Profile</a></li>
</ul>
</nav>
</aside>
<div id="content" class="center">
<h1 id="pageheader">User Profile</h1>
<form id="userprofile">
<table>
<tr>
<td><label for="firstname">First Name: </label></td>
<td><input type="text" id="firstname"></td>
</tr>
<tr>
<td><label for="lastname">Last Name: </label></td>
<td><input type="text" id="lastname"></td>
</tr>
<tr>
<td><label for="email">Email: </label></td>
<td><input type="email" id="email"></td>
</tr>
<tr>
<td><label for="telnumber">Telephone Number: </label></td>
<td><input type="tel" id="telnumber"></td>
</tr>
<tr>
<td><label for="birthdate">Birthdate: </label></td>
<td><input type="date" id="birthdate"></td>
</tr>
<tr>
<td><label for="city">City: </label></td>
<td><input type="text" id="city"></td>
</tr>
<tr>
<td><label for="state">State: </label></td>
<td><select id="state">
<option value="al">AL</option>
<option value="ak">AK</option>
<option value="az">AZ</option>
<option value="ar">AR</option>
<option value="ca">CA</option>
<option value="co">CO</option>
<option value="ct">CT</option>
<option value="de">DE</option>
<option value="fl">FL</option>
<option value="ga">GA</option>
<option value="hi">HI</option>
<option value="id">ID</option>
<option value="il">IL</option>
<option value="in">IN</option>
<option value="ia">IA</option>
<option value="ks">KS</option>
<option value="ky">KY</option>
<option value="la">LA</option>
<option value="me">ME</option>
<option value="md">MD</option>
<option value="ma">MA</option>
<option value="mi">MI</option>
<option value="mn">MN</option>
<option value="ms">MS</option>
<option value="mo">MO</option>
<option value="mt">MT</option>
<option value="ne">NE</option>
<option value="nv">NV</option>
<option value="nh">NH</option>
<option value="nj">NJ</option>
<option value="nm">NM</option>
<option value="ny">NY</option>
<option value="nc">NC</option>
<option value="nd">ND</option>
<option value="oh">OH</option>
<option value="ok">OK</option>
<option value="or">OR</option>
<option value="pa">PA</option>
<option value="ri">RI</option>
<option value="sc">SC</option>
<option value="sd">SD</option>
<option value="tn">TN</option>
<option value="tx">TX</option>
<option value="ut">UT</option>
<option value="vt">VT</option>
<option value="va">VA</option>
<option value="wa">WA</option>
<option value="wv">WV</option>
<option value="wi">WI</option>
<option value="wy">WY</option>
</select></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Save Your Information!"></td>
</tr>
</table>
</div>
<footer>Safe and friendly place to share your love of your pets with others! Design by Lindsey Schreiner.<br /> Forget your user name or password? <a href="##">Click here for help</a>.</footer>
</div>
</body>
I don't know if I'm placing something wrong or if I've missed something stupid, but I've looked in as many places as I could find. If someone could please help see what ever I've missed I'd really appreciate it, thank you.
document.addEventListener("DOMContentLoaded", function(event) { //do work });<body>tag.