4th Unit CSS
4th Unit CSS
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.
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">
<title>Create Cookie</title>
</head>
<body>
<script>
function createCookie() {
alert("Cookie Created");
</script>
</body>
</html>
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">
<title>Read Cookie</title>
</head>
<body>
<script>
function getCookie(name) {
let ca = decodedCookie.split(';');
let c = ca[i].trim();
if (c.indexOf(cname) == 0) {
return "";
</script>
</body>
</html>
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">
<title>Delete Cookie</title>
</head>
<body>
<script>
function deleteCookie() {
alert("Cookie Deleted");
</script>
</body>
</html>
• toGMTString(): Returns a string representing the date in GMT (Greenwich Mean Time).
Syntax:-
Where:
Example code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function changeWindowContent() {
</script>
</body>
</html>
Example code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<body>
<script>
function openMultipleWindows() {
</script>
</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">
<title>setTimeout Example</title>
</head>
<body>
<script>
function showAlert() {
}
// Calling setTimeout to delay the alert
</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">
<title>setInterval Example</title>
</head>
<body>
<script>
function displayTime() {
</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">
<title>Display Pathname</title>
</head>
<body>
<script>
</script>
</body>
</html>