Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can i use search() function to find the cookie is present or not ?

In the level 11 in javascript tutorial in studytonight ,I stuck with the problem .problem is :
You have to do the following:

Fetch the cookie for the current path and save it in the cookie variable on line number 2.

In the if condition, use the search() function to search the cookie variable to find if the string "Hello=World" is present in it or not.

If the cookie is found, we just log it, else, create a cookie with the value "Hello=World". No need to set expiration time or path, just provide the value.

Then, again fetch the cookie in the cookie variable to see if the value is correctly set and we will log its value in the console.

Ignore the other Cookies that you see. And we are done.

and code is "<html>
<head>
<script>
// fetch cookie to check
let cookie = document.cookie ;
if(search(cookie) == "Hello=World" ){
console.log("Cookie found");
}
else {
document.cookie="Hello=World";
}
// fetch cookie to print value
cookie = document.cookie ;



document.write(cookie);
</script>
</head>
<body>

</body>
</html>".please help me ...
by