Response is used to write content to the client (the respondent's browser), including headers and cookies. Here we will only cover the Write method and the property used to set cookies.
Write
Response.Write(arg)
You may use the Write method for debugging purposes, for example to get values of variables you are using in your script written to the screen when testing to help identifying why the script does not work as intended. For other purposes it is recommended to use response piping with ^-s inside one of the text fields of a question or an info instead, since you will then have better control of where on the page the text you output is placed.
arg can be any type of object, as it will be converted to a string when written to the client.
Cookies
Response.Cookies
The Cookies property indicates the cookies collection, which allows addition of cookies to the outgoing stream, adding cookies to the client from a Forsta Plus survey. It can for example be used to prevent respondents of an open survey from taking the survey more than once, as showed in the example below.
The class HttpCookiesCollection provides a wrapper for a collection of cookies. You use this to set and modify properties and values in the cookies. Most useful for us is the Add method:
cookiesCollection.Add(cookie)
The Add method allows the addition of a single cookie to the collection. The parameter cookie is an object of the HttpCookie class. HttpCookie has two different constructors:
varcookie = new HttpCookie(string);which creates and names a new cookie.
varcookie = new HttpCookie(string1, string2);which creates, names (string1), and assigns a value (string2) to a new cookie.
There is currently a problem with the JScript.NET compiler that gives a type mismatch error when compiling with an HttpCookie object. For this reason the variable should be defined as Object to avoid this checking:
var cookie : Object = new HttpCookie(string);or
var cookie : Object = new HttpCookie(string1, string2);Here are the most relevant properties of HttpCookie:
cookie.Expires
The Expires property indicates the expiration date and time of the cookie, as a .NET DateType value (a bit different than the JScript .NET Date object. Consult a .NET reference for more details.) After this expiration date and time, the cookie will not be sent with the request, so you can not retrieve it anymore.
cookie.NameName indicates the name of the cookie.
cookie.PathCookies are specific to the site they originate from, so client browsers only send cookies to the DNS domain from which they were created – i.e. a www.microsoft.com cookie can not be picked up by survey.confirmit.com. But you can set a cookie to indicate the directory path on the domain that should receive it using the Path property. Using a Path of "/" indicates that all directory paths on the server should have access to the cookie.
cookie.Value
The Value property indicates the value for the cookie.
The majority of cookies are used as a single name and value. However, a cookie can have more than one value. The Values property allows you to set several values and also retrieve them again as a NameValueCollection:
cookie.ValuesHere is an example on how to set a cookie:
var myCookie : Object = new HttpCookie("LastVisit");
var now : DateTime = DateTime.Now; //current time
myCookie.Value = now.ToString(); //convert to string
myCookie.Expires = now.AddMonths(6);
myCookie.Path = "/";
Response.Cookies.Add(myCookie);The value of the cookie can later be fetched like this:
var myCookie : Object = Request.Cookies("LastVisit");
if(myCookie != null)
{
var last = myCookie.Value;
//The variable last will now have the timestamp of the last access
}Using Cookies to Limit Access to an Open Survey
Sometimes the sample for a survey is not known in advance, so that you can not set up the survey as a limited survey with cryptic, individual links. Then you have to distribute the open link to the survey instead. However, you can use cookies to try to prevent the respondents from answering more than once. This is not a bullet-proof method of course, because respondents may have set their browsers to not accepting cookies, they may delete cookies and they may also respond from different PCs,
You can set the cookie from a script node anywhere in the survey depending on how far you think the respondents should have answered before not being allowed to reenter. A cookie using the project id as name can be set with this script:
var interval = 3;
var expiry = new Date();
expiry.setMonth(expiry.getMonth()+interval);
var objCookieObject : HttpCookie;
objCookieObject = new HttpCookie(CurrentPID());
objCookieObject.Value = "true";
objCookieObject.Expires = expiry.getVarDate();
objCookieObject.Path = "/" ;
Response.Cookies.Add(objCookieObject);
In the beginning of the survey you may then have a condition that uses this expression to check if the cookie is present:
Request.Cookies[CurrentPID()] != nullIf we also wanted to check the value we would also add that to the expression (not really necessary in this scenario):
Request.Cookies[CurrentPID()] != null && Request.Cookies[CurrentPID()].Value == "true"