如何显示Cookie的输出?

时间:2017-07-13 20:58:11

标签: javascript cookies

您好我有一个问题:

我的网站上有完整功能的Cookie,但我不知道如何显示用户写给他们的内容

我的代码如下:

version: '3'

services:
  service1:
    build:  /home/ubuntu/myapp/service1

  service2:
    build:  /home/ubuntu/myapp/service2

我需要将其显示在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 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 checkCookie() { var user=getCookie("username"); if (user != "") { } else { user = prompt("BlaBlaBla",""); if (user != "" && user != null) { setCookie("username", user, 30); } } } 标记处:

<h1>

我想要显示的Cookie名称是&#34;用户名&#34;或&#34;用户&#34;

我很高兴有任何关于建立cookie的建议,因为我整天搜索互联网而且我不知道建议

1 个答案:

答案 0 :(得分:1)

获取cookie,然后将其注入html ...我会在html h1标签中添加一个ID。

var user = getCookie("username");

//This won't really work because the id isn't called that
var h1 = document.getElementById("heading white");
h1.innerHTML = user;