简体   繁体   English

如何在浏览网页时保存信息?

[英]How to save information while moving through web pages?

How would I keep the information in a shopping cart if I move through pages, without the info being refreshed?如果我浏览页面而不刷新信息,我如何将信息保存在购物车中? Is it possible with just JavaScript?是否可以仅使用 JavaScript?

You can use sessionStorage or localStorage to save some data in the browser if you just want to use Javascript.如果您只想使用 Javascript,您可以使用 sessionStorage 或 localStorage 在浏览器中保存一些数据。 https://javascript.info/localstorage https://javascript.info/localstorage

You can use javascript cookies.您可以使用 javascript cookie。 For set cookies you can use a function like this:对于设置 cookie,您可以使用这样的函数:

 function setCookie(cname, cvalue, exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires="+ d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; }

And for read your cookie in other pages you can use this function:要在其他页面中读取您的 cookie,您可以使用此功能:

 function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for(var i = 0; i <ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; }

Also, I suggest you read this link for more details.另外,我建议您阅读此链接以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM