How to set and get cookie in Java script? [Resolved]

Posted by Patel28rajendra under ASP.NET on 3/22/2012 | Points: 10 | Views : 1497 | Status : [Member] | Replies : 5
Hi

I want to write cookie using java script and then read with java script function .

Thanks

R D Patel


Responses

Posted by: Hmanjarawala on: 3/22/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi rajendra here is a script:


<script type="text/javascript">
<!--
/* Copyright http://www.perlscriptsjavascripts.com
Free and commercial Perl and JavaScripts */

function getCookie(w){
cName = "";
pCOOKIES = new Array();
pCOOKIES = document.cookie.split('; ');
for(bb = 0; bb < pCOOKIES.length; bb++){
NmeVal = new Array();
NmeVal = pCOOKIES[bb].split('=');
if(NmeVal[0] == w){
cName = unescape(NmeVal[1]);
}
}
return cName;
}

function printCookies(w){
cStr = "";
pCOOKIES = new Array();
pCOOKIES = document.cookie.split('; ');
for(bb = 0; bb < pCOOKIES.length; bb++){
NmeVal = new Array();
NmeVal = pCOOKIES[bb].split('=');
if(NmeVal[0]){
cStr += NmeVal[0] + '=' + unescape(NmeVal[1]) + '; ';
}
}
return cStr;
}

function setCookie(name, value, expires, path, domain, secure){
document.cookie = name + "=" + escape(value) + "; ";

if(expires){
expires = setExpiration(expires);
document.cookie += "expires=" + expires + "; ";
}
if(path){
document.cookie += "path=" + path + "; ";
}
if(domain){
document.cookie += "domain=" + domain + "; ";
}
if(secure){
document.cookie += "secure; ";
}
}

function setExpiration(cookieLife){
var today = new Date();
var expr = new Date(today.getTime() + cookieLife * 24 * 60 * 60 * 1000);
return expr.toGMTString();
}
// -->
</script>


Mark this as answer, if it helps you

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Patel28rajendra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/22/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,
for more reference, plz visit:
http://www.perlscriptsjavascripts.com/js/cookies.html
http://www.w3schools.com/js/js_cookies.asp
http://techpatterns.com/downloads/javascript_cookies.php
http://www.java2s.com/Code/JavaScript/Development/Cookiesetdeletegetvalueandcreate.htm


Mark this as answer, if it helps you.............

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Patel28rajendra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/22/2012 [Member] Bronze | Points: 25

Up
0
Down

HI PAtel

http://www.quirksmode.org/js/cookies.html

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Patel28rajendra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/22/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Thanks for Reply

R D Patel

Patel28rajendra, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/22/2012 [Member] Bronze | Points: 25

Up
0
Down
Patel28rajendra
U r welcome



Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Patel28rajendra, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response