接上一篇:在获取了uuid后localStorage的存储与提交时localStorage的使用

昨天只写了在请求图形验证码的同时获取uuid,结果忘记了存储,这里补充一下:基于上一篇的情况,在一个ajax获取了uuid,但是ajax之间是不能变量相互使用的,这个时候就需要js的localStorage了,这是js的一个存储空间,具体可以百度,这里可以把uuid放进去在其他的ajax里面使用,localStorage里空间终究有限,所以注意数据删除,但是注意localStorage的数据需要手动删除:

在这里插入图片描述在这里插入图片描述在这里插入图片描述操作就是这样,有不明白的可以留言问我,对你有用就点个赞吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,这是一种获取浏览器不变的唯一标识符的方法。具体实现可以参考以下代码: ```javascript // 生成一个随机的唯一标识符 function generateUUID() { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; } // 检查浏览器是否支持localStorage或IndexedDB function isLocalStorageSupported() { try { var testKey = 'test'; var storage = window.localStorage; storage.setItem(testKey, '1'); storage.removeItem(testKey); return true; } catch (error) { return false; } } function isIndexedDBSupported() { return 'indexedDB' in window; } // 从localStorage或IndexedDB中读取唯一标识符 function getUniqueID() { var uniqueID = ''; if (isLocalStorageSupported()) { var storage = window.localStorage; uniqueID = storage.getItem('uniqueID'); if (!uniqueID) { uniqueID = generateUUID(); storage.setItem('uniqueID', uniqueID); } } else if (isIndexedDBSupported()) { var request = window.indexedDB.open('uniqueID', 1); request.onerror = function(event) { console.log('Failed to open indexedDB'); }; request.onupgradeneeded = function(event) { var db = event.target.result; var objectStore = db.createObjectStore('uniqueID', { keyPath: 'id' }); var uniqueID = generateUUID(); objectStore.add({ id: 1, value: uniqueID }); }; request.onsuccess = function(event) { var db = event.target.result; var transaction = db.transaction(['uniqueID'], 'readwrite'); var objectStore = transaction.objectStore('uniqueID'); var getRequest = objectStore.get(1); getRequest.onsuccess = function(event) { if (getRequest.result) { uniqueID = getRequest.result.value; } else { uniqueID = generateUUID(); objectStore.add({ id: 1, value: uniqueID }); } }; }; } else { uniqueID = generateUUID(); } return uniqueID; } ``` 以上代码中,`generateUUID()`函数用于生成一个随机的唯一标识符,`isLocalStorageSupported()`和`isIndexedDBSupported()`函数用于检查浏览器是否支持localStorage或IndexedDB,`getUniqueID()`函数用于从localStorage或IndexedDB中读取唯一标识符,如果没有则生成一个新的唯一标识符并存储localStorage或IndexedDB中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值