Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript/cookie problem 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
I generally do things with vbscript on the server, but I have an issue and the only thing I can think of to handle it is with javascript, but I can't get it to work. So, here is what I am trying accomplish. I have a cookie dictionary that I set no problem with vbscript, but now I have to update one of the values when the user clicks on a hyper link. I have tried this for the link

Code:
    <% Do until rs.eof 
    dim conid : conid = rs("contact_id")%>
     <li><a href="ministry.asp?conid=<%=conid %>" onclick="setCookie(conid)"><%=rs("name_first") %>&nbsp;<%=rs("name_last") %>&nbsp;<%=rs("position") %>&nbsp;<%=rs("email") %>&nbsp;<%=rs("cell_phone") %>&nbsp;<%=rs("work_phone")%></a>&nbsp;<a href="editcontact.asp?conid=<%=conid %>">edit</a></li>
    <%rs.MoveNext
    Loop %>

and this for the function

Code:
    <script type="text/javascript">
    function setCookie(conid){
    document.cookie=(("YCMReg")("contact_id")=escape(conid));
    }
    </script>

Both of which seem to fail miserably. First, the link does not pull the value that I want into the onclick event call and my function doesn't seem to actually do anything. Help? Please?

WB
 
ok, I realized what I did wrong to get the value in the link, rather than
Code:
<a href="ministry.asp?conid=<%=conid %>" onclick="setCookie(conid)">

it should be

Code:
<a href="ministry.asp?conid=<%=conid %>" onclick="setCookie(<%=conid)%>">

But the function still doesn't work. Not sure how to take the value that I passed in the function call and use that as the cookie value assignment. Still need help.

wb
 
Not true. If you read my reply to this current post, I realized why the variable was not making it into the function call and I fixed it, but the function itself is still not working as I would like it to. Actually, it is not working at all. In vbscript, the function would look something like
Code:
Function setCookie(conid)
response.cookies("YCMReg")("contact_id")=conid
End Function

And I cannot figure out how to write that function in Javascript.

So, can you help me out with that portion of my problem?

Thanks!
WB
 
[&nbsp;]
As tsuji suggests, you need to go back to that page and re-read it very carefully. Once you see the problem, be careful how hard you slap your forehead.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond

Poor people do not hire employees. If you soak the rich, who are you going to work for?
 
Ouch, that hurt!

Tsuji, my apologies, you were right, I missed those single quotes. I am used to not having to add single quotes for numerical values.

Now, having fixed that, the function still does not work, so...

I have tried
Code:
<script type="text/javascript">
function setCookie(conid){
    document.cookie=(("YCMReg")("contact_id")=escape(conid));
    }
</script>

and
Code:
<script type="text/javascript">
    function setCookie(conid){
    var contact_id = escape(conid);
    var cookieName = (("YCMReg")("contact_id"));
    document.cookie = cookieName+"="+contact_id;
    }
</script>

What am I doing wrong?

wb
 
OK, if I write the function like this
Code:
    <script type="text/javascript">
    function setCookie(c_name,value)
    {
    document.cookie=c_name+ "=" +escape(value);
    }
    </script>
and call it like this
Code:
<a href="ministry.asp?conid=<%=conid %>" onclick="setCookie('contact_id','<%=conid%>')">
then it works, but still not quite the way I would like it to. It will create the cookie named contact_id and assign the value to that cookie, but I would like to add the cookie to the cookie dictionary YCMReg so it is accesses via
Code:
request.cookies("YCMReg")("contact_id")
instead of simply
Code:
request.cookies("contact"id")
My understanding is that this is a WSH function, so it should be do-able with javascript, but I can't figure it out.

More help?

wb
 
[0] To properly set a cookie client-side, you have to observe quite rigorously the mandatory part, namely, the name (with or without key) and value, and then the expires property. You may or may not put the path and some other properties.

[0.1] The server-side see a dictionary object for cases where cookies having a name (your "YCMReg") and then key/value pair.
[tt]
response.cookies("YCMReg")("p")="q"
response.cookies("YCMReg")("r")="s"
'etc
[/tt]
[0.2] On the client-side, it is just a string-like object but with rigorous structure, like this.
[tt]
"...; YCMReg=p=q&r=s; expires=...; ..."
[/tt]
[1] Hence, your function would look something like this.
[tt]
function setCookie(c_name,value)
{
var s_cookie,d;
d=new Date();
d.setMonth(d.getMonth()+1); //expire in one month
s_cookie="YCMReg="+escape(c_name)+"="+escape(value)+"; expires="+d.toGMTString();
document.cookie=s_cookie;
}
[/tt]
 
Ahh... Thank you very much. I had tried many ways of combining to get the dictionary, but not that way, it works great. As for the required info, I thought that I could leave off the Expiry date if I just wanted the cookie to expire when they close their browser session?

Thank you,
wb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top