如何使用 JavaScript 建立基於域的 Cookie?
若要建立基於域的 cookie,請在 cookie 中設定域和路徑屬性,如下所示 −
domain=.example.com
示例
您可以嘗試執行以下程式碼來了解如何建立基於域的 cookie −
<html>
<head>
<script>
<!--
function WriteCookie() {
if( document.myform.customer.value == "" ){
alert("Enter some value!");
return;
}
cookievalue= escape(document.myform.customer.value) + ";";
document.cookie = "name=" + cookievalue + ";
domain=.example.com;path=/";
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
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP