Thursday, July 20, 2023

COOKIES

 

COOKIES

Web Browsers and Servers use HTTP protocol to communicate and HTTP is a stateless protocol.

server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie.

Storing Cookies

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

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

    <title>Document</title>

 

    <h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Cookies</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

    <script type="text/javascript">

       

        function WriteCookie()

        {

         if( document.myform.customer.value == "" ){

         alert ("Enter some value!");

         return;

         }

         cookievalue= escape(document.myform.customer.value) + ";";

         document.cookie="name=" + cookievalue;

         document.write ("Setting Cookies : " + "name=" + cookievalue );

        }

     

        </script>

</head>

<body>

    <form name="myform" action="">

        Enter name: <input type="text" name="customer"/>

        <input type="button" value="Set Cookie" onclick="WriteCookie();"/>

        </form>    

       

</body>

</html>

Output

 









<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

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

    <title>Document</title>

 

    <h2 style="background-color: rgb(9, 238, 9);color:rgb(255, 0, 0);font-weight: bolder;font-family: Georgia, 'Times New Roman', Times, serif;">Cookies</h2>

    <h5 style="color: rgb(231, 15, 166);">Output</h5>

    <script type="text/javascript">

   function getCookie(c_name)

{

if(document.cookie.length>0)

 {

 c_start=document.cookie.indexOf(c_name + "=");

 if(c_start!=-1)

 {

 c_start=c_start + c_name.length+1;

 c_end=document.cookie.indexOf(";",c_start);

 if (c_end==-1) c_end=document.cookie.length;

 return unescape(document.cookie.substring(c_start,c_end));

 }

 }

return " ";

}

function setCookie(c_name,value,expiredays)

{

var exdate=new Date();

exdate.setDate(exdate.getDate()+expiredays);

document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString());

}

function checkCookie()

{

username=getCookie('username');

 username=prompt('Please enter your name:',"");

 if (username!=null && username!="")

 {

     alert('Welcome again '+username+'!');

 setCookie('username',username,365);

 }

 

}

        </script>

</head>

<body onload="checkCookie()">

       

       

</body>

</html>

 

Output












0 comments:

Post a Comment