/    Sign up×
Community /Pin to ProfileBookmark

cookies problem

Hi all,
I am newbie to this forum. I am trying set cookies on client m/c using javascript: the code is as below. The fillCookie function fills the cookie values (if present) into the fields on the form. But for some odd reason, when the cookie are not present, the uid field gets filled with a ‘;'(semicolon, i cannot find from where it is getting that) even though there is no cookie present. The pw field works just fine ? . Both use same functions to set and get cookies. The cookies are set from a asp page as:

[CODE]
input id=”myLoginButton” style=”position:relative;top:5;” type=”image” name=”submit” value=”Send” src=”images/login.gif” border=0
onclick=”javascript:setcookie(‘un’,document.forms[0].uid.value,365);setcookie(‘pw’,document.forms[0].pw.value,365);”>[/CODE]

The javascript code is as below:

[CODE]function getcookie(cookiename) {
var cookiestring=””+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1 || cookiename==””) return “”;
var index2=cookiestring.indexOf(‘;’,index1);
if (index2==-1) index2=cookiestring.length;
return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,duration){
cookiestring=name+”=”+escape(value)+”;EXPIRES=”+getexpirydate(duration);
document.cookie=cookiestring;
if(!getcookie(name)){
return false;
}
else{
return true;
}
}

function fillCookie()
{
//if the cookies are present then pull them up
alert(getcookie(“un”));
alert(getcookie(“pw”));
if(getcookie(“un”)==’;’) {
document.forms[0].uid.value=””;
}
else {
document.forms[0].uid.value=getcookie(“un”);
}
document.forms[0].pw.value=getcookie(“pw”);
}[/CODE]

Thanks for your help
K

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Apr 11.2005 — I only perused your code. I see that you need to retrieve the values of TWO cookies and then use those values to fill form fields. Your code indicates that you already know how to address a form field. The folllowing example sets and retrieves the value of two cookies. Hopefully, you will find it useful.

<HTML>

<Head>

<Script Language=JavaScript>

var expDate = new Date;
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year

function setCookie(isName,isValue,dExpires){

document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}

function getCookie(isName){

cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
else
{
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}
}

function dispCookie(isName){

isValue = getCookie(isName);
alert(isValue);
}

function deleteCookie(isName){

if (getCookie(isName)){document.cookie = isName + "="+
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}


</Script>

</Head>

<Body>

<input type=button value='Set 1st Cookie' onclick="setCookie('anyName','hello',expDate)">

&nbsp;<input type=button value='Set 2nd Cookie' onclick="setCookie('someName','goodbye',expDate)">

<br><br>

<input type=button value='Delete 1st Cookie' onclick="deleteCookie('anyName')">

&nbsp;<input type=button value='Delete 2nd Cookie' onclick="deleteCookie('someName')">

<br><br>

<input type=button value='Read 1st Cookie' onclick="dispCookie('anyName')">

&nbsp;<input type=button value='Read 2nd Cookie' onclick="dispCookie('someName')">

</Body>

</HTML>
×

Success!

Help @kdayalu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...