Solved

Expiry date in Javascript?????

Posted on 2001-09-11
25
487 Views
Last Modified: 2008-02-01
Here is some Javascript running on an .asp page everything works fine except setting the expiry date.  


<%@LANGUAGE="JAVASCRIPT"%>
<!--#include file="Connections/MerciaData.asp" -->
<% if(Request.Form("remember") == "yes")
{
var today = new Date()
today.setMonth(today.getMonth + 3);


    Response.Cookies("remember")("username") = Request.Form("username")
    Response.Cookies("remember")("password") = Request.Form("password")
    Response.Cookies("remember")("rememberme") = "yes"
     Response.Cookies("remember").Expires = today;

 }
%>

Once I have ran the code if I tick the box remember I get the following error:

Technical Information (for support personnel)

Error Type:
Microsoft JScript runtime (0x800A000D)
Type mismatch
/mercia/TMP75iw6ji8oi.asp, line 12


Browser Type:
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; DigExt)

Page:
POST 56 bytes to /mercia/TMP75iw6ji8oi.asp

POST Data:
username=sdasd&password=asdas&remember=yes&Submit=Submit

Time:
11 September 2001, 16:36:27

Line twelve is the "Response.Cookies("remember").Expires = today;"  
0
Comment
Question by:mercia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 6
  • +1
25 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 6474863
Why do you insist on tyring to run vbscript as javascript?  Run the code as VBScript and it will work fine!

I thought we discussed this in a previous Q?

There is no such javascript object "Response", or for that matter, anything you have wrapped.

<%@LANGUAGE="VBScript"%>
<!--#include file="Connections/MerciaData.asp" -->
<% if Request.Form("remember") = "yes" Then
today = Date() + 90

   Response.Cookies("remember")("username") = Request.Form("username")
   Response.Cookies("remember")("password") = Request.Form("password")
   Response.Cookies("remember")("rememberme") = "yes"
   Response.Cookies("remember").Expires = today;

%>

0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6474864
Of course you would loose the ; in this line,

Response.Cookies("remember").Expires = today;

0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6476198
Do u mind trying as follows:

Response.Cookies("remember").Expires = today.toString();

I have always worked on javaScript on CLIENT SIDE ONLY. Never on server side.

I think, you are getting error because today is date object and not a date itself. You will need to convert it to string before assigning it.

For complete reference on Client-side and Server-side javaScript, you may wish to visit the following link:

http://developer.netscape.com/docs/manuals/index.html?content=javascript.html

mgfranz,

Frankly, i am curious as to how come "mercia" is not getting error on previous lines of code if Response object is not supported by server-side javaScript?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mercia
ID: 6478755
My reason for trying to use JAVASCRIPT is because everything else is written in Javascript and I do not want to change the whole script to VBSCRIPT. If Response is not an Object in Javasript what is its alternative?  Who do I turn it into JAVASCRIPT?
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6478811
Have you tried the code as suggested by me? If it continue to fail, please let me know.

If you do decide to go along the javaScript path, do download the documentation, it would be highly helpful. Offline htm/pdf version available as .zip file.
0
 

Author Comment

by:mercia
ID: 6479494
If I do as you suggest I will have to change the remaining code which I do not wish to do.  I do not understand what you mean about download.
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6479525
I am afraid, you are not getting me.

I am the one who is attempting to keep you on javaScript track. Please see the solution given by me above. The download i am talking about is for your future help to deal with javaScript programming on either server or client side.

For your reference i am posting my last solution again here...

Response.Cookies("remember").Expires = today.toString();
0
 

Author Comment

by:mercia
ID: 6479594
If I do as you suggest I will have to change the remaining code which I do not wish to do.  I do not understand what you mean about download.
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6479614
Please tell me which scripting language you have finally decided to use. JavaScript or VBScript. As i am getting confused.

mgfranz has suggested VBScript which you are not accepting. I am helping you go on javaScript track, which you are not accepting either.

Kindly clarify your stand and let me know where you are stuck now.

0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6479723
Nitin, this is not javascript;

Response.Cookies("remember").Expires = today.toString();

Never has been, never will be.
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6479798
Dear mgfranz,

As already state before i have NEVER used javaScript before on server-side.

My only hope is "mercia" is getting error on the above line and not others. At least, "mercia" is not reporting that to us.

I tried the code given by "mercia" and me too getting error ONLY on .Expires line. The inteliSense is not showing that property. So, it means the problem is not a langauge, it seems.

However, if i comment that line with // the asp engine doesn't mind about other lines.

And as always i am open.

0
 

Author Comment

by:mercia
ID: 6479811
I have stated that I do not want to change all of the rest of my code.  I thought I made it clear I want to use JAVASCRIPT.  Javascript must be able to set a cookie in an ASP page from data gained from a user, this is all I am trying to do.  I appreciate that I may have my code mixed up but my intent is to have a script that offers the choice of remember me when a user logs in.  The part I am stuck on is the same as before expiry date I have tried all suggested answers and got no where does anyone know how to set a cookie that lasts 1 year based on data entered by a user on to an ASP page using JAVASCRIPT?
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6479827
This isn't even Server-side javascript...

What do you mean change the rest of your code?  Javascript can set a cookie but not with the code you have posted.

Maybe you need to ask your professor for some help... or open the books your parents spent so much money on...
0
 

Author Comment

by:mercia
ID: 6479869
Thanks for the pep talk however there is no professor or parents spending money on books.  I thought Experts exchange was a knowledge base where those with the know how helped those without.  mqfranz you too good to help fine put dont put dont put down those trying to learn!
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6479912
I never said I "was too good", I was saying that I have seen too many test questions asked in the 4 years I have been a member of this group.

If you want to learn, you first need to understand the difference between client-side and server-side scripts.

Then you need to understand javascript, and vbscript and how to incorporate the two.

What you want to do is simple, so think simple.

In ASP/VBScript this would require about 10 lines of code, inn javascript I can only guess it would require about 40, plus at least 2 function calls.

If all you require is a way to have a user "remembered" by a cookie when logging in, all you need is a basic form with a check-box, and the cookie script I posted earlier inside a simple If..Then loop.

If you are learning how to write Javascript and VBScript, I cannot stress this enough, keep it simple, clean, and functional.

There is no need to re-write the book.
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6480007
Mercia,

Well, our first mistake is .Expires is the property of Response object and not that of cookies. Second mistake it expects a numeric input. Read following.

Syntax
Response.Expires [= number]

Parameters
number
The time in minutes before the page expires.

I am still fighting with .ExpiresAbsolute, which accepts date as string, and will get back to you, if successfull.

0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6480045
Mercia,

Husssh...

Here is complete and successfully runing code on my machine.

<%@Language=JavaScript%>
<%
var tday = new Date();
tday.setMonth(tday.getMonth() + 3);
var strToday = new String(tday.toDateString());
Response.Cookies("remember")("username") = Request.Form("username")
Response.Cookies("remember")("password") = Request.Form("password")
Response.Cookies("remember")("rememberme") = "yes"
Response.ExpiresAbsolute = strToday.substr(4, strToday.length-4);
%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Response.Write("Hi!");
Response.Write(tday + "<BR>");
Response.Write(strToday.substr(4, strToday.length-4));
%>
<P>&nbsp;</P>
</BODY>
</HTML>
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6480052
Nitin, do you know what you are talking about?  Do you even know ASP and VBScript?

Expires is write-only and is the date on which the cookie expires. Unless a date is specified, the cookie will expire when the session ends. If a date is specified, the cookie will be stored on the client's disk after the session ends.

The ExpiresAbsolute property sets the precise date and time when a response page cached on the browser will expire. If the time is not given, then the page will expire exactly at midnight on the given date. If the date is not given, then the page will expire at the given time on the same day that the script is run.

So, Expires is an object of cookies, ExpiresAbsolute is not!

I have used the following cookie script for at least two years now without a failure;

<%
Response.Cookies("Wine")("WineUser")=WineUser
Response.Cookies("Wine")("WinePass")=WinePass
Response.Cookies("Wine")("WineID") = WineID
Response.Cookies("Wine").Expires = Date + 7
Response.Clear
Response.Redirect "wine.asp?Action="& WhatAction &""
%>

Maybe you need to go back to the books...
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6480078
First of all, this is not Javascript;

<%@Language=JavaScript%>
<%
var tday = new Date();
tday.setMonth(tday.getMonth() + 3);
var strToday = new String(tday.toDateString());
Response.Cookies("remember")("username") = Request.Form("username")
Response.Cookies("remember")("password") = Request.Form("password")
Response.Cookies("remember")("rememberme") = "yes"
Response.ExpiresAbsolute = strToday.substr(4, strToday.length-4);
%>


Second of all, it does not work, no cookie file is wrote.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6480100
Again... This works;

<%@Language=VBScript%>
<%
tday = Date()

Response.Cookies("remember")("username") = "M"
Response.Cookies("remember")("password") = "F"
Response.Cookies("remember")("rememberme") = "yes"
Response.Cookies("remember").Expires = tday + 90
%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Response.Write("Hi!")
Response.Write(tday & "<BR>")

%>
0
 
LVL 9

Expert Comment

by:Nitin Sontakke
ID: 6480245
Dear mgfranz,

Now see the code below, this works just fine on my local host. It even printed the name back which i typed as username.

I am confused as to why you do not accept which IIS seem running on localhost is accepting perfectly well.

I also fail to understand why you don't call it a javaScript at all.

Mercia,

Could you please implement the modified code by me as follows (please just pick-up code as per your requirement):

<%@Language=JavaScript%>
<%
var tday = new Date();
tday.setMonth(tday.getMonth() + 3);
var strToday = new String(tday.toDateString());
Response.Cookies("remember")("username") = Request.Form("username")
Response.Cookies("remember")("password") = Request.Form("password")
Response.Cookies("remember")("rememberme") = "yes"
Response.Cookies("remember").Expires = strToday.substr(4, strToday.length-4);
%>
<HTML>
<BODY>
<Form Name=TempForm Method="Post" Action="temp.asp">
<Input Type=Text Name="username">
<Input Type=Text Name="password">
<Input Type=Submit Name="cmdSubmit">
<%
Response.Write("Hi!");
Response.Write(tday + "<BR>");
Response.Write(strToday.substr(4, strToday.length-4));
Response.Write(Request.Cookies("remember")("username"));
%>
<P>&nbsp;</P>
</Form>
</BODY>
</HTML>
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6480283
Hmm... First you say that this is running on your machine;

<%
var tday = new Date();
tday.setMonth(tday.getMonth() + 3);
var strToday = new String(tday.toDateString());
Response.Cookies("remember")("username") = Request.Form("username")
Response.Cookies("remember")("password") = Request.Form("password")
Response.Cookies("remember")("rememberme") = "yes"
Response.ExpiresAbsolute = strToday.substr(4, strToday.length-4);
%>

Which obviously it does not...

Then you say to run this;

<%
var tday = new Date();
tday.setMonth(tday.getMonth() + 3);
var strToday = new String(tday.toDateString());
Response.Cookies("remember")("username") = Request.Form("username")
Response.Cookies("remember")("password") = Request.Form("password")
Response.Cookies("remember")("rememberme") = "yes"
Response.Cookies("remember").Expires = strToday.substr(4, strToday.length-4);
%>

Which is different code.  Frankly I'm not sure I trust anything you say at the moment...

Besides, I believe you are mixing JScript with Javascript...
0
 

Expert Comment

by:enrico_ballistico
ID: 6509170
mercia,

I'm in the same boat...I've got a big site that I have implimented using asp with the javascript scripting language and now I wish to setup cookies on the sucker and hey presto I've got the same problems...

What I have done in the meantime (till I figure it out either way) is to put my results in to session variables and call a new page written using asp with the VB scripting language then pick them up from there...this works, I haven't had to redo my site and I'm sure the performance of the site is hardly compromised...

I await your comments...especially you mgfranz!
0
 
LVL 18

Accepted Solution

by:
mgfranz earned 50 total points
ID: 6509376
To write a cookie that expires in 90 days that is stored on the client;

<%@Language=VBScript%>
<%
tday = Date()

Response.Cookies("remember")("username") = "M"
Response.Cookies("remember")("password") = "F"
Response.Cookies("remember")("rememberme") = "yes"
Response.Cookies("remember").Expires = tday + 90
%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%
Response.Write("Hi!")
Response.Write(tday & "<BR>")

%>

If you insist on using javascript to create a cookie;

<script language="javascript">

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// date variables for setting new cookie
     var now = new Date(); // create an instance of date
     fixDate(now); // fix the bug in Navigator 2.0, Macintosh
     now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); // cookie expires in one year, in milleseconds

myDir = myDirectory; // navigation based on cookie

// set a new cookie with new date
   setCookie("construct",myDir,now, '/','.domain.com');
</script>

Hasn't this Q gone on long enough... especially for only 50 points!

You have your solutions, let's close this Q.
0
 

Author Comment

by:mercia
ID: 6509432
Thanks that has helped solve the problem
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question