Introduction to JavaScript Cookies
Cookies के द्वारा user की information store की जाती है। Normally जब user किसी website को visit करता है तो web server के pass उसकी कोई information नहीं होती है। लेकिन cookies के द्वारा किसी भी user की information store करना संभव है।

Cookie एक normal text file होती है। जब भी कोई user किसी website को visit करता है तो उसकी information cookies (text file) के रूप में store कर ली जाती है। ये information user के computer में ही store की जाती है।



भविष्य में जब भी user वापस उस website के लिए request करता है तो user की request के साथ उस user की cookie भी webserver को भेजी जाती है।



इस प्रकार web server को उस user की information प्राप्त हो जाती है। इस information के आधार पर webserver को उस user की preferences के बारे में पता रहता है। साथ ही इस information के आधार पर web server web pages में जरुरी बदलाव भी कर सकता है।



JavaScript आपको cookies create करने, read करने, change करने और delete करने की ability provide करती है। इसके लिए JavaScript में document.cookie property available है।



Creating Cookies with JavaScript 

JavaScript में cookies create करने का general syntax निचे दिया जा रहा है। 

document.cookie = "name=value; expiry-date; path";


  • name=value - जब भी आप cookie में कोई information store करते है तो ऐसा आप name और value के pair में करते है। उदाहरण के लिए यदि आप user की ID store करना चाहते है तो इसके लिए आप "Id=101" define करेंगे। 
  • expiry-date - आप cookies की expiry date भी set करते है। जो date आप define करेंगे उस date के आते है की cookie automatically delete हो जायेगी। Expiry date को expires attribute द्वारा set किया जाता है। 
  • path - इस parameter के द्वारा आप ये define करते है की cookie किस path से related है। यदि कोई path नहीं दिया हुआ है तो इसका मतलब है की cookie current page से related है।  


Example 

JavaScript में cookie create करना निचे उदाहरण के द्वारा समझाया जा रहा है।



<html>

<script type="text/javascript">



document.cookie = "userName=John;expires=Fri, 26 May 2017 09:00:00 UTC;path=/";

document.cookie = "Id=101;expires=Fri, 26 May 2017 09:00:00 UTC;path=/";



document.write("Cookies are set.."+"<br>");



</script>

</html>


ऊपर दी गयी script निचे दिया गया output generate करती है।



Cookies are set...


Reading Cookies with JavaScript

JavaScript में आप set की गयी cookies को आसानी से read कर सकते है। इसके लिए आप किसी normal variable को document.cookie property assign करते है। ये property उस page पर set की गयी सभी cookies को name और value के pair में return करती है।



इसका उदाहरण निचे दिया जा रहा है। 

<html>

<script type="text/javascript">



document.write("Reading cookies..."+"<br>");

var myCookie = document.cookie;

document.write(myCookie);



</script>

</html>

ऊपर दी गयी script output के रूप में set की गयी सभी cookies को name और value के pair में return करती है।



Changing Cookies with JavaScript 

JavaScript में किसी भी cookie को change करने के लिए आप उसे वापस दूसरी values के साथ create करते है। उदाहरण के लिए आप ऊपर create की गयी cookies को इस प्रकार change कर सकते है। 

<html>

<script type="text/javascript">



document.cookie="userName=Sara;expires=Fri, 26 May 2017 9:00:00 UTC;path=/";

document.cookie="Id=103;expires=Fri, 26 May 2017 9:00:00 UTC;path=/";



document.write("Cookies are changed...";



</script>

</html>


ऊपर दी गयी script निचे दिया गया output generate करती है।



Cookies are changed...


Deleting Cookies with JavaScript 

JavaScript द्वारा किसी भी cookie को delete करने के लिए आप उस cookie को दुबारा create करते है। Cookie को दुबारा create करते समय आप उसकी value नहीं देते है। साथ ही expires parameter में कोई पुरानी date value के रूप में pass करते है।



उदाहरण के लिए यदि आप ऊपर create की गयी userName और Id cookies को delete करना चाहते है तो ऐसा आप इस प्रकार करेंगे। 

<html>

<script type="text/javascript">



document.cookie="userName=;expires=Wed, 3 May 2017 09:00:00 UTC;path=/";

document.cookie="Id=;expires=Wed, 3 May 2017 09:00:00 UTC;path=/';



document.write("Cookies are deleted...");



</script>

</html>

ऊपर दी गयी script निचे दिया गया output generate करती है।



Cookies are deleted... 

A Complete Example of Cookies with JavaScript

<html>

<script type="text/javascript">



document.cookie = "userName=John;expires=Fri, 26 May 2017 09:00:00 UTC;path=/";

document.cookie = "Id=101;expires=Fri, 26 May 2017 09:00:00 UTC;path=/";



document.write("Cookie are set.."+"<br>");



document.write("Reading cookies..."+"<br>");



var myCookie = document.cookie;



document.write("Showing cookies.."+"<br>");



document.write(myCookie+"<br>");



document.write("Changing cookies.."+"<br>");



document.cookie="userName=Sara;expires=Fri, 26 May 2017 9:00:00 UTC;path=/";

document.cookie="Id=103;expires=Fri, 26 May 2017 9:00:00 UTC;path=/";



document.write("Cookies are changed..."+"<br>");



document.write("Showing cookies.."+"<br>");



var myCookieAgain = document.cookie;



document.write(myCookieAgain+"<br>");



document.write("Deleting cookies.."+"<br>");



document.cookie="userName=;expires=Wed, 3 May 2017 09:00:00 UTC;path=/";

document.cookie="Id=;expires=Wed, 3 May 2017 09:00:00 UTC;path=/";



document.write("Cookies are deleted...");



</script>

</html>

ऊपर दी गयी script निचे दिया गया output generate करती है।



Cookies are set..
Reading cookies..
Showing cookies..
userName=John,Id=101
Changing cookies..
Cookies are changed.
Showing cookies...
userName=Sara,Id=103
Deleting Cookies.
Cookies are deleted.