本文介绍了Java脚本:使用多个提示并通过cookie打印它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,这是我第一次在这个网站上发帖提问。我有一个java脚本,询问即将到来的用户他的名字和宠物名称。然后显示他们访问本网站的名称和宠物名称及次数,我可以在每次访问时打印访问的名称和次数。但无法打印出宠物名称。这是我第一次使用cookies。这是我的代码: 我尝试过: function setCookie(cname,cvalue,exdays){ var d = new 日期(); 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 日期(); 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 encodedCookie = decodeURIComponent( document .cookie); var ca = decodingCookie.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 encodedCookie = decodeURIComponent( document .cookie); var cr = decodingCookie.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( 再次欢迎 + user); document .getElementById( demo).innerHTML = 欢迎回来, + user; document .getElementById( d).innerHTML = 您的宠物名称, + pet; } else { user = prompt( 请输入您的姓名:, ); pet = prompt( 请输入您的宠物名称:, ); if (user!= && user!= null ){ setCookie( username,user, 30 ); setCookie2( pet,user, 30 ); } } } 解决方案 情侣,我觉得getCookie2和setCookie2是多余的。这行应该是setCookie(pet,pet,30);而不是setCookie(pet,user,30); function checkCookie(){ var user = getCookie( 用户名); var pet = getCookie( pet ); if (user!= ){ alert( 再次欢迎 + user + ' 和你的宠物:' + pet); document .getElementById( demo).innerHTML = 欢迎回来, + user; document .getElementById( d).innerHTML = 您的宠物名称, + pet; } else { user = prompt( 请输入您的姓名:, ); pet = prompt( 请输入您的宠物名称:, ); if (user!= && user!= null ){ setCookie( username,user, 30 ); setCookie( pet,pet, 30 ); } } } 输出: 欢迎回来,myname 您的宠物名称,mypet Hello everyone, its my first time posting question on this site. 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:What I have tried: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); } }} 解决方案 Couple thing, I think the getCookie2 and setCookie2 are redundant. and this line should be setCookie("pet", pet, 30); instead of setCookie("pet", user, 30);function checkCookie() { var user = getCookie("username"); var pet = getCookie("pet"); if (user != "") { alert("Welcome again " + user + ' and your pet: ' + pet); 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); setCookie("pet", pet, 30); } }}Output:Welcome back, mynameYour pet name, mypet 这篇关于Java脚本:使用多个提示并通过cookie打印它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-17 00:02