0% found this document useful (0 votes)
3 views

4th Unit CSS

The document provides an overview of cookies in web development, including how to create, read, and delete cookies using JavaScript. It also explains various JavaScript functions such as getMonth(), setMonth(), toGMTString(), setTimeout(), and setInterval(), along with examples for manipulating browser windows and displaying information. Additionally, it covers the use of the window.location object to access the current pathname of a web page.

Uploaded by

ifzalsolkar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4th Unit CSS

The document provides an overview of cookies in web development, including how to create, read, and delete cookies using JavaScript. It also explains various JavaScript functions such as getMonth(), setMonth(), toGMTString(), setTimeout(), and setInterval(), along with examples for manipulating browser windows and displaying information. Additionally, it covers the use of the window.location object to access the current pathname of a web page.

Uploaded by

ifzalsolkar01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1] What is cookies?

Cookies are small pieces of data stored by the web browser on a user's computer. They are used to
remember information between page loads or visits, such as login credentials, user preferences, or
tracking information for analytics.

2] Explain how to create cookies in JavaScript.

In JavaScript, cookies can be created by setting the document.cookie property. The cookie is stored in
the browser and sent to the server with every request to the domain.

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Create Cookie</title>

</head>

<body>

<script>

// Function to create a cookie

function createCookie() {

document.cookie = "username=JohnDoe; expires=Thu, 31 Dec 2024 12:00:00 UTC; path=/";

alert("Cookie Created");

</script>

<button onclick="createCookie()">Create Cookie</button>

</body>

</html>

3] Explain how to read cookies in JavaScript.

To read cookies, you can use the document.cookie property. It returns a string of all cookies stored in
the browser, separated by semicolons.

Example code:-

<!DOCTYPE html>

<html lang="en">
<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Read Cookie</title>

</head>

<body>

<script>

// Function to read a specific cookie by name

function getCookie(name) {

let cname = name + "=";

let decodedCookie = decodeURIComponent(document.cookie);

let ca = decodedCookie.split(';');

for(let i = 0; i < ca.length; i++) {

let c = ca[i].trim();

if (c.indexOf(cname) == 0) {

return c.substring(cname.length, c.length);

return "";

alert("Cookie value: " + getCookie("username"));

</script>

</body>

</html>

4] Write a JavaScript to delete the cookie.

To delete a cookie, you can set its expires attribute to a past date.

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Delete Cookie</title>

</head>

<body>

<script>

// Function to delete a cookie

function deleteCookie() {

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";

alert("Cookie Deleted");

</script>

<button onclick="deleteCookie()">Delete Cookie</button>

</body>

</html>

5] Explain the functions getMonth(), setMonth(), and toGMTString().

• getMonth(): Returns the month (0–11) for the specified date.

• setMonth(): Sets the month (0–11) for a specified date object.

• toGMTString(): Returns a string representing the date in GMT (Greenwich Mean Time).

6] Explain the syntax for opening a window.

The window.open() method is used to open a new browser window or tab.

Syntax:-

window.open(URL, name, specs, replace);

Where:

• URL: The URL to open in the new window.

• name: The name of the window.

• specs: A comma-separated list of window features (e.g., size, position).

• replace: If true, it replaces the current page in the window.

• 7] Explain the use of focus() method for the window object.


• The focus() method is used to bring the specified window or tab into focus. If the
window is minimized or in the background, calling focus() will bring it to the front.
• 8] Write a JavaScript to change the contents of the newly created
window.
• You can change the content of a window by manipulating the document object of that
window.

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Change Window Content</title>

</head>

<body>

<script>

// Function to create a new window and change its content

function changeWindowContent() {

let newWindow = window.open("", "NewWindow", "width=500,height=500");

newWindow.document.write("<h1>This is the new content!</h1>");

</script>

<button onclick="changeWindowContent()">Open and Change Window Content</button>

</body>

</html>

9] Write a JavaScript to open multiple windows at a time.

You can open multiple windows by calling window.open() several times.

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Open Multiple Windows</title>


</head>

<body>

<script>

// Function to open multiple windows

function openMultipleWindows() {

window.open("https://www.google.com", "_blank", "width=500,height=500");

window.open("https://www.bing.com", "_blank", "width=500,height=500");

window.open("https://www.yahoo.com", "_blank", "width=500,height=500");

</script>

<button onclick="openMultipleWindows()">Open Multiple Windows</button>

</body>

</html>

10] What is the use of setTimeout() function? Write a JavaScript to illustrate it.

The setTimeout() function calls a function or executes a code snippet after a specified number of
milliseconds.

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>setTimeout Example</title>

</head>

<body>

<script>

// Function to demonstrate setTimeout()

function showAlert() {

alert("This message appears after 3 seconds!");

}
// Calling setTimeout to delay the alert

setTimeout(showAlert, 3000); // 3000 milliseconds = 3 seconds

</script>

</body>

</html>

11] What is the use of setInterval() function? Write a JavaScript to illustrate it.

The setInterval() function calls a function or executes a code snippet repeatedly at specified intervals
(in milliseconds).

Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>setInterval Example</title>

</head>

<body>

<script>

// Function to demonstrate setInterval()

function displayTime() {

let date = new Date();

document.body.innerHTML = "<h1>" + date.toLocaleTimeString() + "</h1>";

// Calling setInterval to update the time every second

setInterval(displayTime, 1000); // 1000 milliseconds = 1 second

</script>

</body>

</html>

12] Write a JavaScript to display the pathname of the web page using window.location object.

You can access the pathname of the current URL using window.location.pathname.
Example code:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Display Pathname</title>

</head>

<body>

<script>

// Displaying the pathname of the current page

alert("Current pathname: " + window.location.pathname);

</script>

</body>

</html>

You might also like