您好,欢迎访问一九零五行业门户网

如何在 JavaScript 中唯一标识访问网站的计算机?

每当我们创建任何应用程序或网站时,我们都需要唯一地识别访问该网站的计算机。唯一标识计算机有很多好处。
例如,您正在向用户提供一些服务。通过唯一标识计算机,当用户首次从新设备访问您的网站时,您可以提供免费试用服务。当用户再次访问时,您可以要求用户购买高级版或订阅您的应用程序。
在这里,我们将使用 cookie 来识别访问网站的计算机。
什么是 cookie?cookie 允许开发人员在浏览器中存储用户信息。例如,我们可以将数据从服务器发送到浏览器并存储在浏览器中。因此,每当用户重新访问该网站时,它都会从 cookie 而不是从服务器获取数据。因此,cookie 可以提高应用程序的性能。
在我们的例子中,当用户第一次访问网站时,我们可以将 cookie 设置为 100 年到期。此后,每当用户再次访问该网站时,我们都会检查cookie是否存在,然后我们就可以说用户重新访问了该网站。
语法用户可以按照以下语法在网络浏览器上设置和获取 cookie。
// to set cookiesdocument.cookie = isvisited=true;// to get cookieslet ca = decodeuricomponent(document.cookie).split(';');
在上面的语法中,我们将一个带有键值对的字符串分配给 document.cookie 以将 cookies 设置到浏览器中。要获取 cookie,我们可以简单地使用 document.cookie,它返回 cookie 数组。
步骤第 1 步 - 创建 fetchcookies() 函数。
第 2 步 - 在 fetchcookies() 函数中,使用 document.cookie 获取数组格式的 cookie,并使用decodeuricomponent() 方法对 cookie 进行解码。
第 3 步 - 使用 for 循环迭代数组。
步骤 4 - 对于数组的每个元素,删除数组开头的空格。
第 5 步 - 使用 indexof() 方法检查数组元素是否包含第 0th 索引处的键,并使用 substring() 获取键值方法。
第 6 步 - 返回特定键的值。
第 7 步 - 创建 fetchcookies() 函数。在 fetchcookies() 函数中,调用 getcookie() 函数并检查 cookie 是否存在。
第 8 步 - 如果 cookie 为空,则设置 cookie。
第9步 - 根据所需的cookie是否为空打印消息。
示例在下面的示例中,每当用户第一次访问网站时,我们都会在 cookie 中将“isvalidate”设置为“true”值。每当用户第二次访问该网站时,我们都会在 cookie 中获取“isvalidate”,因此我们会打印“欢迎回到网站”之类的消息。
<html><body> <h3>using the <i> cookies </i> to uniquely identify computers visiting web site in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to get cookies function fetchcookies(cname) { let key = cname + =; let ca = decodeuricomponent(document.cookie).split(';'); for (let i = 0; i < ca.length; i++) { let part = ca[i]; while (part.charat(0) == ' ') { part = part.substring(1); } if (part.indexof(key) == 0) { return part.substring(key.length, part.length); } } return null; } // set cookies to uniquely identify the computer visiting the website function checkcookies() { var cookies = fetchcookies(isvisited); if (cookies == null) { content.innerhtml = welcome to the website; document.cookie = isvisited=true; } else { content.innerhtml = welcome back to the website; } } checkcookies(); </script></body></html>
示例在下面的示例中,每当用户第一次访问该网站时,我们都会使用提示框询问他们的姓名并显示欢迎消息。此外,我们将 cookie 设置为 100 年有效期。
每当用户第二次访问时,我们都会显示带有他们姓名的欢迎消息,而不会询问他们的姓名。
<html><body> <h3>using the <i> cookies </i> to uniquely identify computers visiting web site in javascript</h3> <div id = content> </div> <script> let content = document.getelementbyid('content'); // function to get cookies function fetchcookies(cname) { let key = cname + =; let ca = decodeuricomponent(document.cookie).split(';'); for (let i = 0; i < ca.length; i++) { let part = ca[i]; while (part.charat(0) == ' ') { part = part.substring(1); } if (part.indexof(key) == 0) { return part.substring(key.length, part.length); } } return null; } // set cookies to uniquely identify the computer visiting the website function checkcookies() { var cookies = fetchcookies(customcookie); if (cookies == null) { let name = prompt(enter your name, shubham); document.cookie = customcookie= + name + ; expires=thu, 23 oct 2120 12:00:00 utc; path=/; content.innerhtml = how are you + name + ?; } else { content.innerhtml = hey, + cookies + you visited our site again!; } } checkcookies(); </script></body></html>
用户学会了使用 javascript 中的 cookie 来唯一标识访问网站的计算机。然而,cookie 有一些限制。如果用户清除cookie,我们就无法唯一地识别该计算机。另外,我们需要将 cookie 的有效期限设置为 100 年。此外,如果用户使用不同的浏览器,我们无法唯一地识别计算机。
克服上述所有问题的最佳解决方案是使用 google analytics。
以上就是如何在 javascript 中唯一标识访问网站的计算机?的详细内容。
其它类似信息

推荐信息