当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Java script: using multiple prompts and printing them through cookies

问题描述:

I have a java script asking the coming user for his name and pet name. Then display the name and pet name and number of times they visit this site, I am able to print the name and number of visit at each visit. But unable to print out the pet name. Its my first time working with cookies. Here is my code:

function setCookie(cname,cvalue,exdays) {

var d = new Date();

d.setTime(d.getTime() + (exdays*24*60*60*1000));

var expires = "expires=" + d.toGMTString();

document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";

}

function setCookie2(cpet,cvalue,exdays) {

var e = new Date();

e.setTime(d.getTime() + (exdays*24*60*60*1000));

var expires = "expires=" + e.toGMTString();

document.cookie = cpet + "=" + cvalue + ";" + expires + ";path=/";

}

function getCookie(cname) {

var name = cname + "=";

var decodedCookie = decodeURIComponent(document.cookie);

var ca = decodedCookie.split(';');

for(var i = 0; i < ca.length; i++) {

var c = ca[i];

while (c.charAt(0) == ' ') {

c = c.substring(1);

}

if (c.indexOf(name) == 0) {

return c.substring(name.length, c.length);

}

}

return "";

}

function getCookie2(cpet) {

var pet = cpet + "=";

var decodedCookie = decodeURIComponent(document.cookie);

var cr = decodedCookie.split(';');

for(var i = 0; i < cr.length; i++) {

var d = cr[i];

while (d.charAt(0) == ' ') {

d = d.substring(1);

}

if (d.indexOf(name) == 0) {

return d.substring(name.length, d.length);

}

}

return "";

}

function checkCookie() {

var user=getCookie("username");

var pet=getCookie2("pet");

if (user != "") {

alert("Welcome again " + user);

document.getElementById("demo").innerHTML = "Welcome back, " + user;

document.getElementById("d").innerHTML = "Your pet name, " + pet;

} else {

user = prompt("Please enter your name:","");

pet = prompt("Please enter your pet name:","");

if (user != "" && user != null) {

setCookie("username", user, 30);

setCookie2("pet", user, 30);

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: