この例では、ユーザにプロンプトで名前を尋ね、Cookieを使用して「Welcome、savedCookie!入力した後、そしてそれらが満了前にページを訪れるたびに。JavaScriptでCookieを設定、読み込み、書き込む
私はブラウザに自分の名前を尋ねるプロンプトを表示し、それからdivに「ようこそ、未定義!」と書きます。私はなぜエラーを受け取るのか分かりません。
//Setting cookie for Users Name
function set_it() {
var userName = prompt("Please Enter Your Name", ""),
thetext = "name=" + userName,
expdate = ";expires=Mon, 27 Mar 2017 13:00:00 UTC",
newtext = encodeURIComponent(thetext);
newtext += expdate;
document.cookie = newtext;
}
//Reading & Writing cookie
function read_it() {
var rawCookie = document.cookie;
bakedCookie = decodeURIComponent(rawCookie);
yumCookie = bakedCookie.split("=");
document.getElementById("greeting").innerHTML = "<p>Welcome, " + yumCookie[1] + "!</p>";
}
if (document.cookie) {
read_it();
} else {
set_it();
read_it();
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="greeting">
<script src="prjs13_2.js"></script>
</div>
</body>
</html>
あなたの返信ありがとう!私はもう少しテストを行いました。ブラウザの設定で、必要な効果をブロックする必要があります。私はフィドルでそれをテストし、それが私がそれをEdgeでテストした後に、それが必要なものを正確に行い、また望ましい効果を得ました。 –