如何使用 JavaScript 仅为主页设置 cookie?
要为主页设置 cookie,请将页面添加到主页本身。
示例
你可以尝试运行以下代码来设置 cookie
<html>
<head>
<script>
<!--
function WriteCookie() {
if( document.myform.customer.value == "" ){
alert("Enter some value!");
return;
}
cookievalue= escape(document.myform.customer.value) + ";";
var myPath = window.location.pathname;
var address = query.split("/");
if ((address[address.length - 1]).toLowerCase() == "home.html") {
document.cookie = "name=" + cookievalue;
document.write ("Setting Cookies : " + "name =" + cookievalue);
}
}
//-->
</script>
</head>
<body>
<form name = "myform" action="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onclick="WriteCookie();"/>
</form>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP