当前位置:网站首页>JS存取cookie示例
JS存取cookie示例
2022-07-27 07:26:00 【奔跑的狮子a】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="js/jquery-2.2.1.min.js"></script>
<body>
<input id="username" />
<button id="write">赋值cookie</button>
<button id="read">取值cookie</button>
<span id="cookie"></span>
</body>
</html>
<script> $("#write").click(()=>{
let username = $("#username").val(); console.log(username); setCookie("userName",username,1); }) $("#read").click(()=>{
//cookie取值: var cookie = getCookie("userName"); console.log(cookie) $("#cookie").html(cookie); }) /** * 默认的cookie写入方法 * @param name cookie的key 例子:userName * @param value cookie的value 例子:张三 * @param time cookie的存放时间,以'天'为单位 例子:1 */ function setCookie(name,value,time){
var Days = time; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ encodeURI (value) + ";expires=" + exp.toGMTString()+";path=/"; } /** * 获取Cookie中的值 * @param objName * @returns */ function getCookie(objName){
//获取指定名称的cookie的值 var arrStr = document.cookie.split("; "); for (var i = 0; i < arrStr.length; i++) {
var temp = arrStr[i].split("="); if (temp[0] == objName){
return decodeURI(temp[1]); } } } </script>
边栏推荐
- Cadence (XI) silk screen printing adjustment and subsequent matters
- What are the main threads of Youxuan database?
- Chapter 6 Shell Logic and Arithmetic
- 【pytorch】ResNet18、ResNet20、ResNet34、ResNet50网络结构与实现
- 我是不是被代码给耽误了……不幸沦为一名程序员……
- Examples of Oracle triggers
- 小程序支付管理-新版支付对接流程
- Showdoc vulnerability learning - cnvd-2020-26585 (arbitrary file upload)
- C winfrom common function integration-2
- C language programming | program compilation and preprocessing
猜你喜欢

闭散列和开散列解决哈希冲突

Codeforces Round #810 (Div.2) A-C

Framework of electronic mass production project -- basic idea

Comprehensive analysis of ADC noise-02-adc noise measurement method and related parameters
为啥国内大厂都把云计算当成香饽饽,这个万亿市场你真的了解吗

Perl: split the external command to be executed into multiple lines

STM32_找到导致进入HardFault_Handler的函数

Record a pit dug by yourself~

C common function integration-3

An open source OA office automation system
随机推荐
Showdoc vulnerability learning - cnvd-2020-26585 (arbitrary file upload)
Expose Prometheus metrics in Perl programs
用户解锁SM04 SM12
Demonstrate the use of foreign keys with Oracle
Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query
Chapter 6 Shell Logic and Arithmetic
Understanding and learning of properties class and properties configuration file
Oracle cleans up the Database disk space of tables with referenced partitions
What about idea Chinese garbled code
Pg_ relation_ Size question
单元测试系统化讲解之Mockito
SQL statement batch update time minus 1 day
Oracle composite query
Compiling and using log4cxx in rhel7.3
[golang learning notes 2.1] sorting and searching in arrays in golang
Systematic explanation of unit testing: mockito
利用 Amazon DynamoDB 和 Amazon S3 结合 gzip 压缩,最大化存储玩家数据
The solution of using sqlplus to display Chinese as garbled code
ARP broadcasting practice cases
Solution to automatic disconnection of SSH link of Tencent ECS