Codementor Events

Amazing cookies in JavaScript hacks ?

Published Mar 03, 2020
Amazing cookies in JavaScript hacks ?

Start writing heA Cookie is a Set of Information that operates between Client Side and Server Side. A Web browser completely Stores Data at certain Time of Browsing.
Cookie Mainly has Data, that is like String, which is generally in Form of a Name – Value by Certain Semi-colons. It handles, a certain state of User and it remembers user Data will all type of pages. for more selenium online training
How these cookies Work
If User Send a Request, to Server, then every request seen, like request. Many Different users send that.
The Browser at the side of the Client.
If a user sends a Request, to Server, the cookie is completely added with a Request in an Automatic way. That is due to Cookie, the server recognize the user.
How we can Design a Cookie
In Java Script, we can design, read, update and Delete certain cookie by utilizing Document. Cookie and property.
The following Syntax is utilised to design a Cookie.
document.cookie="name=value";
The below is the example, you can go through it…
Example 1
Let's see an example to set and get a cookie.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>

Example 2

Here, we display the cookie's name-value pair separately.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin";
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
selenium online training Hyderabad offers Some type of Portals and Certain Attributes, that enhance functions in a Automatic way. You have some type of lists and Attributes with certain description.
Attributes and Description
Domain – it is used to specify a domain for every cookie that is Valid.
Path – It expands the complete scope of cookie to every Page of website.
Max –age – It Handles state of a cookie, that is up to certain specified time. Here the time offered in seconds.
Expires – It handles the state of Cookie that specified with date and time.
Cookie Expire Attribute
The cookie Expire attribute offer only one way to design and persistent Cookie. Date and time were declared, which represent an active period of cookie.
Once the declared time passed away, and cookie deleted automatically.

Cookie Expire Attribute Example
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin;expires=Sun, 20 Aug 2030 12:00:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>

Cookie Max age Attribute
This feature offer another Direction to design a Persistent cookie. The Time is showed in seconds, a cookie is valid up to declared and initiated time by itself.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin;max-age=" + (60 * 60 * 24 * 365) + ";"
}
Function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
Cookie Path Attribute
Now let us Understand, the direction, that with guidance of this example.
Here, if we design a cookie, for webpage2.html, it is valid only by itself and for its sub Directory. It is not a valid webpage1.html Document.
In this example, we utilize, a path Attribution for Enhancing the visibility of these cookies. That is up to every page. Get Depth Knowledge On selenium training
Here, you have to manage and maintain directory design.
<script>
function setCookie()
{
document.cookie="username=Duke Martin;max-age=" + (60 * 60 * 24 * 365) + ";path=/;" }
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
Cookie Domain Attribute
A Java Script attribute specify, domain for which it require certain valid. For instance if we offer any Domain name or any attribute, that is like
Domain = OnlineITguru.com
Here the cookies are validated and up to the mark with all its sub domains.
Not to mention, if we offer any sub-domain to attribute like.
Omain=training.Javatpoint.com
re...

Discover and read more posts from pravallika Bandaru
get started
post commentsBe the first to share your opinion
Show more replies