This is a JavaScript function that retrieves the value of a cookie by its name. It works by accessing the document.cookie property, which contains all the cookies for the current page, and then splitting the string to isolate the value of the desired cookie.

Here's how it works:

  • The function takes a 'name' parameter, which is the name of the cookie you want to retrieve.
  • It initializes the 'value' variable with a semi-colon followed by the entire document.cookie property. The semi-colon is added to make sure that the search for the cookie name is accurate, as cookie names can be present in multiple places within document.cookie.
  • It splits the 'value' string using the cookie name as a separator, which creates an array with two parts. The first part is everything before the cookie value, and the second part is the cookie value followed by any remaining cookie information.
  • It checks that the array has two parts (i.e., the cookie name was found), and if so, it returns the second part of the array (the cookie value) after splitting it again by the semi-colon separator and taking the first part (to remove any remaining cookie information).

Overall, this function provides a simple way to access the value of a specific cookie within a web page.

JavaScript Function to Get Cookie Value by Name

原文地址: https://www.cveoy.top/t/topic/mr1o 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录