Module4
Module4
What is cookies?
In JavaScript, cookies are small pieces of data that are stored on the user's
browser. These cookies are often used for tracking, storing session
information, user preferences, and other data that needs to persist across
different pages or sessions. JavaScript provides simple methods to work with
cookies.
document.cookie = "user=JohnDoe";
Setting a Cookie with Expiration: You can specify the expiration date of a
cookie. If the expires attribute is not set, the cookie will expire when the
session ends (i.e., when the user closes the browser).
// Set a cookie that expires in 1 day
const expires = new Date();
expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000)); // 1 day in
milliseconds
document.cookie = "user=JohnDoe; expires=" + expires.toUTCString();
Setting a Cookie with Path and Domain: You can specify a path for which the
cookie is valid. By default, cookies are only available to the page that created
them, but you can extend the scope with the path attribute.
document.cookie = "user=JohnDoe; path=/; expires=" + expires.toUTCString();
you can also set the domain attribute to specify for which domains the cookie
is available.
Reading a Cookie: To read a cookie, you can simply access document.cookie.
However, document.cookie returns all cookies in a single string. You'll need to
parse it manually.
// Function to get a cookie value by name
function getCookie(name) {
Deleting a Cookie: To delete a cookie, set its expiration date to a past date.
This effectively invalidates the cookie.
// Delete the "user" cookie by setting its expiration date to the past
Example Code:
// Set a cookie
// Get a cookie
function getCookie(name) {
// Delete a cookie
function deleteCookie(name) {
// Example usage
Notes:
Cookie Size: Cookies are limited in size (about 4KB per cookie).
Security: Be cautious with sensitive data in cookies, as they can be
intercepted by malicious users if not secured.
SameSite Cookies: You can control cross-site request handling using the
SameSite attribute to prevent CSRF attacks.
function getCookie(name) {
function deleteCookie(name) {
// Example usage
// Create (Set) a cookie named "user" with value "JohnDoe" for 7 days
deleteCookie('user');
Browser :
In JavaScript, you can interact with the browser window by opening new
windows, giving them focus, setting their position, changing their
content, and closing them. Below is an example that demonstrates these
tasks:
1. Opening a New Window
You can use the window.open() method to open a new window. This
method takes several parameters to control the window's
characteristics.
2. Giving the Window Focus
Once the new window is opened, you can use the focus() method to give
the window focus.
3. Changing the Window's Position
You can control the position of the new window by using the
window.moveTo() method. This method allows you to specify the x and y
coordinates of the window.
4. Changing the Content of the Window
You can access the content of the new window and modify it by using
document.write() or other DOM manipulation methods.
5. Closing the Window
newWindow.focus();
// 3. Changing the position of the window
newWindow.document.write('<button onclick="window.close()">Close
Window</button>');
// 5. Closing the window after 5 seconds (you can manually close it too)
setTimeout(() => {
newWindow.close();
Explanation:
Important Notes: