Cookies: Cookies are small text files stored on a user’s device by a website. They are used to store information about the user, such as preferences, login credentials, and shopping cart contents so that the website can retrieve that information during future visits. Cookies are often used for session management, to keep track of a user’s session as they navigate through a website.
Cookies are stored in the user’s browser and are sent back to the server with each subsequent request to the website, allowing the server to access the stored information. Cookies can be set to expire after a specified amount of time or can persist until they are deleted by the user.
Cookies can also be used for tracking and advertising purposes, as they allow websites to store information about a user’s browsing behavior and use that information to deliver targeted advertisements. This has led to some concerns about privacy and the use of cookies. However, many browsers now provide users with the option to control or block cookies, either on a site-by-site basis or globally.
Location of cookies on the hard disk: Cookies are small text that is stored on a user’s computer or device by a website. The location where cookies are stored on a hard disk varies depending on the web browser being used. In general, cookies are stored in a specific folder or directory on the user’s computer.
For example, chrome stores all cookies in a single file called cookies. To find the chrome cookie’s location in the computer we have to open the file browser open c drive then go into users then the open folder with the administrator name there you find the App Data folder in that we have to go to the Local folder then in that we have to go to Google folder in that we have to open chrome folder in Chrome we have to open user data folder in that there will be a folder with name default there we see our system cookies.
File Path(Google): C:\Users\Your_User_Name\AppData\Local\Google\Chrome\User Data\Default.
File Path(Microsoft): C:\Users\Your_User_Name\AppData\Local\Microsoft\Edge\User Data\Default.
Note: For Microsoft Edge same path till the local folder in the local, we have to go into the Microsoft folder in which we open the Edge folder then the user Data then the default folder.
The cookies information inside the cookie file is not human-readable. If we want to see and manage the cookies, we have to use the browser’s interface
The user can access these folders and view or delete the cookies stored there. However, it’s worth noting that some cookies are essential for the proper functioning of some websites, so be careful when deleting them.
Some modern browsers also support storing cookies in the browser’s built-in memory, meaning that the cookie data is deleted once the browser is closed. These is known as session cookies, it’s mainly used for authentication and session management.
Example: Set and get cookies using JavaScript
- Create a new text file and save it with a .html extension, for example, cookie-example.html
Open the file in a text editor, such as Notepad or Sublime Text - Copy and paste the code into the file
- Save the file
- Open the file in a web browser, such as Google Chrome or Mozilla Firefox
- Open the developer console in the web browser. You can do this by right-clicking on the page and selecting “Inspect” or “Inspect
- Element”, then clicking on the “Console” tab.
- To get output, type document.cookie press enter in the console, which should show the cookie username we created “John Doe”.
Note: Keep in mind that cookies are stored on the client side, so you’ll need to open the HTML file in a web browser on each individual device you want to test the code on.
Javascript
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=/" ; } // Retrieving a cookie function getCookie(cname) { var name = cname + "=" ; var ca = document.cookie.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 "" ; } // Example usage: setCookie( "username" , "John Doe" , 30); console.log(getCookie( "username" )); // Outputs "John Doe" |
Output:
JavaScript is a client-side programming language that runs in the user’s web browser, not on the server or the user’s hard disk. This means that JavaScript has limited access to the user’s device, and it can only interact with information that is stored in the browser’s memory, such as cookies.
Cookies are small text files stored by the browser on the user’s device, and they are used to store small amounts of data, such as user preferences or login information, between browser sessions. JavaScript can read and write cookies, but it does not have direct access to the user’s hard disk. Instead, it can only manipulate the data stored in the browser’s memory, including cookies.
For security reasons, JavaScript is designed to have limited access to the user’s device, and it cannot access or manipulate data stored on the hard disk or any other physical storage. This is to prevent malicious scripts from compromising the user’s privacy and security by accessing sensitive information stored on the device.