Heim  >  Artikel  >  Web-Frontend  >  Wie kann der Computer, der eine Website besucht, in JavaScript eindeutig identifiziert werden?

Wie kann der Computer, der eine Website besucht, in JavaScript eindeutig identifiziert werden?

WBOY
WBOYnach vorne
2023-09-13 12:33:03665Durchsuche

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

每当我们创建任何应用程序或网站时,我们都需要唯一地识别访问该网站的计算机。唯一标识计算机有很多好处。

例如,您正在向用户提供一些服务。通过唯一标识计算机,当用户首次从新设备访问您的网站时,您可以提供免费试用服务。当用户再次访问时,您可以要求用户购买高级版或订阅您的应用程序。

在这里,我们将使用 cookie 来识别访问网站的计算机。

什么是 cookie?

Cookie 允许开发人员在浏览器中存储用户信息。例如,我们可以将数据从服务器发送到浏览器并存储在浏览器中。因此,每当用户重新访问该网站时,它都会从 cookie 而不是从服务器获取数据。因此,cookie 可以提高应用程序的性能。

在我们的例子中,当用户第一次访问网站时,我们可以将 cookie 设置为 100 年到期。此后,每当用户再次访问该网站时,我们都会检查cookie是否存在,然后我们就可以说用户重新访问了该网站。

语法

用户可以按照以下语法在网络浏览器上设置和获取 cookie。

// to set cookies
document.cookie = "isVisited=true";

// to get cookies
let 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。

Das obige ist der detaillierte Inhalt vonWie kann der Computer, der eine Website besucht, in JavaScript eindeutig identifiziert werden?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen