b) Ask the user for their name and then append to the visitor_cookie the user\'s
ID: 3868134 • Letter: B
Question
b) Ask the user for their name and then append to the visitor_cookie the user's name. If the user has never been to the website before, prompt them with a welcome message.
Example: User enters Jukka. The browser pops up a message: Welcome Jukka! This is your first visit.
c) Write a function called visitors that outputs the number of times each unique visitor has been to the website. That is, your output list should not contain any repetitions of names. Sample output might be:
Joe: 5 times Joanna: 3 times Cub: 7 times
a) Your website may contain several cookies, but they are all in format: cookie_name = value1^value2 Write a function called visitor_array which takes no arguments and returns an array of the names in the visitor cookie. The visitor cookie has format: visitor_cookie = Joe^Joanna^Cub In the example above, your function should return an array where first entry is Joe, second Joanna third Cub.Explanation / Answer
<!DOCTYPE html>
<html>
<head>
<script>
function visitor_cookie(userName,val,_days) {
var d = new Date();
d.setTime(d.getTime() + (_days*24*60*60*1000));
var expireTime = "expires on " + d.toGMTString();
document.cookie = userName + " = " + val + " ; " + expireTime + " ;path=/";
document.arr.push({userName:1});
}
function getCookie(userName) {
var _name = userName + "=";
var visitor_cookies = decodeURIComponent(document.cookie);
var data = visitor_cookies.split(';');
for(var i = 0; i < data.length; i++) {
var _cname = data[i];
while (_cname.charAt(0) == ' ') {
_cname = _cname.substring(1);
}
if (_cname.indexOf(_name) == 0) {
return _cname.substring(_name.length, _cname.length);
}
}
return "";
}
function startProcess(){
while(true){checkCookieExist();}
}
function checkCookieExist() {
if(!document.arr) {
document.arr = [];
}
var userName=getCookie("username");
if (userName != "") {
var visit_times = document.arr[userName];
alert("Welcome back " + userName+"!");
document.arr[userName] = visit_times+1;
alert("You visited "+visit_times+" times");
}
userName = prompt("Please enter your _name:","");
if (userName != "" && userName != null) {
visitor_cookie("username", userName, 30);
alert("Welcome " + userName+"! This is your first visit.");
}
}
</script>
</head>
<body>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.