1,cookie
cookie的话可以加载Jquery和JQuery.cookie 模块
temp.html页面
<body>
<button onclick="jump()">jump</button>
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery.cookie.min.js"></script>
<script>
var userName = 'ReSword';
var userPassword = '111111';
var test = "xxx";
function jump () {
// path: '/' 表示在整个域名下都可由使用
// cookie的使用必须建立在服务上
$.cookie('userName', userName, { path: '/' });
$.cookie('userPassword', userPassword, { path: '/' });
window.location.href = './test.html';
}
</script>
</body>
test.html页面
<body>
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery.cookie.min.js"></script>
<script>
console.log($.cookie('userName'), $.cookie('userPassword'));
</script>
</body>

2,参数
temp.html页面代码
<body>
<button onclick="jump()">jump</button>
<script>
var uerName = 'ReSword';
var userPassword = '111111';
function jump () {
window.location.href = `./test.html?userName=${uerName}&userPassword=${userPassword}`;
}
</script>
</body>
test.html页面代码
<body>
<script>
var url = location.href;
var results = decodeURI(url.substring(url.indexOf('?')+1,url.length)).match(/\w+/g);
var obj = {};
results.forEach((val, index) => {
if (index % 2) obj[results[index - 1]] = val;
});
console.log(obj);
</script>
</body>

3.localStorage
temp.html页面
<body>
<button onclick="jump()">jump</button>
<script>
var userName = 'ReSword';
var userPassword = '111111';
var myStorage = localStorage;
function jump () {
myStorage.setItem('userName', userName);
myStorage.setItem('userPassword', userPassword);
window.location.href = './test.html';
}
</script>
</body>
test.html页面
<body>
<script>
var myStorage = localStorage;
console.log(myStorage.getItem('userName'), myStorage.getItem('userPassword'));
</script>
</body>

Cookie与localStorage页面应用示例
博客介绍了cookie和localStorage在页面中的应用。对于cookie,可加载Jquery和JQuery.cookie模块,并给出temp.html和test.html页面示例;同时也展示了localStorage在temp.html和test.html页面的应用情况。

被折叠的 条评论
为什么被折叠?



