2. [20pts] JS cookies and DOM Note: We will not bother with cookie expiration ti
ID: 3843628 • Letter: 2
Question
2. [20pts] JS cookies and DOM
Note: We will not bother with cookie expiration time in this question.
a) Your website uses only one cookie called dates. This cookie contains dates in form
Your page has the following markup:
a) Write a function load_cookie() that loads into the two text fields the dates from the cookie if the cookie is not empty.
b) Recall that we can call a function when the submit button on the form is pressed. Write a function save_cookie that stores the two dates into the dates cookie overwriting whatever dates were in the cookie already. You may assume that the two fields are always filled.
Explanation / Answer
Find the java script program below:
var delimiter = "#";
function save_cookie(date1, date2) {
var separator = "#" // or whatever
var values = [date1, date2].join(separator);
// then you store the values in a cookie
document.cookie = "myValues=" + values;
}
function load_cookie(myValues) {
var values = myValues + "=";
var whole_cookie = document.cookie.split(values)[1].split(";")[0];
return whole_cookie;
}
function Submit() {
var date1 = document.getElementById('date1').value;
var date2 = document.getElementById('date2').value;
save_cookie(date1,date2);
var myDate = load_cookie('myValues');
var myDateArray = myDate.split(delimiter);
var cookieDate1 = myDateArray[0];
var cookieDate2 = myDateArray[1];
alert('The cookie created has the following information stored on it: Date1: ' + cookieDate1 + ' Date2: ' + cookieDate2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.