Use VBScript write cookie and get cookie

this is my code to write cookie using VBScript, but it can not work.

expiredate = date() + 360
document.cookie = "Ki=" & sel & ";expires=" & expiredate

the following is how I retieve the cookie:

for i= 0 to ubound(chkcookie)
if instr(chkcookie(i),"Ki")>0 then
end if
Who is Participating?
These two functions will set and get a cookie.

<script language="VBScript" type="text/vbscript">
<!-- // cloak
sub setcookie(cookiename,cookievalue,plusmonths)
  document.cookie = cookiename & "=" & cookievalue & "; expires " & dateAdd("M",plusmonths,Now)
end sub

sub getcookie(cookiename)
  mycookies = document.cookie
  cstart = instr(mycookies,cookiename)
  if not cstart < 1 then
    clength = len(cookiename) + 1
    cend = instr(cstart,mycookies,";")
    cvalue = mid(mycookies,cstart+clength,cend-clength-cstart)
    msgbox("Cookie not set")
  end if
end sub
// decloak -->
<a href="#" onclick="getcookie 'Ki'">Get cookie</a><br>
<a href="#" onclick="setcookie 'Ki','Value 2','12'">Set cookie</a>

After strugling with it myself I discovered you can't have "expires=".  It needs to be "expires ".  I also use the dateAdd function and not date() + 360.

I am assuming of course you mean client side.  Server side is much easier if you want to do it that way.
sub setcookie(cookiename,cookievalue,plusmonths)
  response.cookies(cookiename) = cookievalue
  response.cookies(cookiename).expires = dateAdd("M",plusmonths,Now)
end sub

sub getcookie(cookiename)
end sub
Here is the code for setting and getting the cookie using javascript.

   <script language="Javascript">
      function setcookievalue(name,value){
               document.cookie=name+"="+value+";"+"expires=Wednesday, 19-Aug-01 23:12:40 GMT"

      function getcookievalue(name){
         var temp=document.cookie+";";
         var Pos=temp.indexOf("=",temp.indexOf(name+"="));
         if (temp.indexOf(name+"=")==-1) return "";
         return temp.substring(Pos+1,temp.indexOf(";",Pos));
   <form name="Example">
      Cookie name:<input name="CName" type="text" size=20><br>
      Cookie value:<input name="CVal" type="text" size=40><br>
      <input type="button" value="Set Cookie" onClick="setcookievalue(document.Example.CName.value,document.Example.CVal.value)">
      <input type="button" value="Get Cookie" onClick="document.Example.CVal.value=getcookievalue(document.Example.CName.value)">
      <input type="button" value="View document.cookie" onClick="document.Example.ViewCookie.value=document.cookie"><br>
      document.cookie:<input name="ViewCookie" type="text" size=50>

Hope this maybe helpful to you.
jyxAuthor Commented:
I modified my VBScript as this:
document.cookie = "KiPrinter=" & selprinter & ";expires=01-Jul-05 GMT"
now it can work on my machine, but can not work on another machine I tested. I am confused.
These 2 machines are not using same IE version.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The testing I did and scripts I have seen do not have an "=" after expires.  Try:
document.cookie = "KiPrinter=" & selprinter & ";expires 01-Jul-05 GMT"

jyxAuthor Commented:
thanks for all your help.
Now it can work here.
document.cookie = "KiPrinter=" & selprinter & ";expires=Monday, 20-Jan-03 23:12:40 GMT"
jyxAuthor Commented:
The problem is whom should I give the points to? Thanks!
Points can go to who you like.  Or you can split.  Go to Community Support (link on the left) and post a 0 point question saying you want to split points.  Say how you want to split them and they will give you instructions.  Remember to post a link to this question in your CS question.

I do suggest not going with a literal expiry if you can help it though.  The script I wrote lets you expire it however many months after now as you like.  I can explain if you like.
jyxAuthor Commented:
I tried that DateAdd thing but it just did not work. I will try it later. thanks.
Glad to help.  Thanks for the A.
From the Accepted Solution, In the sub 'getcookie' I had an issue with the 'mid' statement when I only had one cookie created. This may help others if they have the same issue...

With only one cookie created, the variable 'cend' equals '0' because it can't find a ';' (semi-colon) in the 'document.cookie' output string.

This output would show 'ki=value 2' (notice no semi-colon at the end, as there is only one cookie no semi-colon separator is needed between cookies) so this meant that the 'cend = instr(cstart,mycookies,";")' part failed as it couldn't find the semi-colon.

The solution I came up with was to change the 1st line of this sub from:

mycookies = document.cookie


myCookies = document.cookie + ";"

Which adds the semi-colon onto the end of the 'document.cookie' string so that the 'cend = instr(cstart,mycookies,";")' part can actually find a semi-colon and return the correct end value. Therefore making the output string show 'ki=value 2;'.

I thought this maybe helpful to someone! Cheeers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.