什么是网络上的Cookies,您如何使用它们?

操纵饼干 (Manipulating Cookies)

Getting or setting cookies is a straightforward operation that can be achieved by accessing the cookie property on the browser’s document object.

获取或设置cookie是一项简单的操作,可以通过访问浏览器文档对象上的cookie属性来实现。

Let's say you find an amazing and informative recipe website to cook a fun meal for your guests but it’s in foreign language. Luckily you are able to change the language on the website using a dropdown.

假设您找到了一个令人惊叹且信息丰富的食谱网站,为您的客人烹制一顿有趣的饭菜,但它是外语。 幸运的是,您可以使用下拉菜单更改网站上的语言。

A couple of days later you visit the same site again to make a dish for your mother, but now you see the website in your native language by default.

几天后,您再次访问同一站点为母亲做菜,但现在默认情况下您会以您的母语看到该网站。

How'd that happen? The website remembers the language you selected on your last visit and stores it in the form of a cookie. Now it automatically selects your preferred language by reading that cookie.

怎么会这样 该网站会记住您上次访问时选择的语言,并将其存储为cookie形式。 现在,它会通过读取Cookie自动选择您的首选语言。

userLanguage:french

userLanguage:french

Cookies are used to store data in the form of name:value pairs on the client side. They let a website store user specific information on the browser for later use. The stored information could be sessionID, userCountry, visitorLanguage and so on.

Cookies用于在客户端以name:value对的形式存储数据。 他们让网站将用户特定的信息存储在浏览器中,以供以后使用。 存储的信息可以是sessionIDuserCountryvisitorLanguage等。

Another way to store the data on the client side is localstorage.

在客户端存储数据的另一种方法是localstorage

A cookie can be set using the syntax below. But a library, like the one mentioned at the end, is highly recommended to make development easier for everyone.

可以使用以下语法设置Cookie。 但是,强烈建议您像结尾处提到的那样使用一个库,以使每个人都更容易开发。

While setting the cookie, you can set the expiration date for it as well. If you skip this, the cookies are erased when the browser is closed.

设置cookie时,您也可以设置其过期日期。 如果您跳过此步骤,则在关闭浏览器时将清除cookie。

Keep in mind that a cookie set by a particular domain can only be read by that domain & its subdomains only.

请记住由特定域设置Cookie只能由该域及其子域读取。

// Using vanilla javascript
document.cookie = 'userLanguage=french; expires=Sun, 2 Dec 2017 23:56:11 UTC; path=/';

//Using JS cookie library
Cookies.set('userLanguage', 'french', { expires: 7, path: '/' });

The above cookie expires in 7 days.

上面的cookie将于7天后过期。

// Using vanilla javascript
console.log(document.cookie)

// => "_ga=GA1.2.1266762736.1473341790; userLanguage=french"

// Using JS cookie library
Cookies.get('userLanguage');

// => "french"

In order to delete a cookie set the expiration date to something in the past.

为了删除Cookie,请将过期日期设置为过去的某个日期。

// Using vanilla javascript
document.cookie = 'userLanguage; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';

//Using JS cookie library
Cookies.remove('userLanguage');

If you find yourself playing with cookies a lot in your project, please use a library like this JS Cookie and save yourself a ton of time.

如果您发现自己在项目中经常使用Cookie ,请使用像JS Cookie这样的库来节省大量时间。

翻译自: https://www.freecodecamp.org/news/what-are-cookies-on-the-web-and-how-do-you-use-them/

### 回答1: Cookies的服务目的是为了让用户在访问同一网站时无需重复输入信息,同时也可以提供更加个性化的服务和广告。Cookies可以记录用户访问网站的历史记录、用户的偏好设置等信息,以便在用户下次访问网站时为用户提供更好的服务。 ### 回答2: 掌握信息在网络的存在方式主要包括通过网络搜索引擎、网站、社交媒体等平台获取信息,以及通过电子邮件、即时通讯工具等进行信息传递和交流。 其中,搜索引擎是最常用的获取信息的方式之一。用户通过输入关键词,搜索引擎会从互联网上抓取相关的网页,然后按照一定的算法对这些网页进行排序和展示,用户可以从搜索结果中找到自己感兴趣的内容。 另外,各种网站也是用户获取信息的重要来源。不同类型的网站提供各种各样的信息和服务,它们可以是新闻、博客、论坛、在线商店等。用户可以通过浏览网站的方式获取所需的信息,也可以通过注册成为会员或订阅等方式获取更多专属内容。 此外,社交媒体也为用户获取信息提供了便利。用户可以通过关注、点赞、评论等方式与其他用户交流,获取他人分享的信息、观点和经验。 而cookies的服务目的主要有两个方面。一方面,cookies可以帮助网站记录用户的偏好和习惯,提供个性化的服务。例如,网站可以利用cookies记住用户的登录状态、购物车内容和阅读历史,从而提供更快捷、更个性化的浏览体验。 另一方面,cookies还可以用于统计分析和广告定向。网站可以借助cookies分析用户的行为和喜好,从而优化网站的布局和功能,改进用户体验。同时,cookies也可以用于广告定向,根据用户的兴趣和偏好展示相关的广告内容。 总而言之,掌握信息在网络的存在方式包括搜索引擎、网站和社交媒体等平台,而cookies的服务目的是提供个性化的服务和实现统计分析与广告定向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值