Session variable

E

Enoch Chan

I would like to set a Session variable to a value. In Vbscript it should be

Session("ZoomValue")=500

How can I set this session variable by using Javascript?

Thanks
 
J

Jim Ley

I would like to set a Session variable to a value. In Vbscript it should be

Session("ZoomValue")=500

How can I set this session variable by using Javascript?

Session.ZoomValue=500

Jim.
 
R

Robert

Enoch Chan said:
I would like to set a Session variable to a value. In Vbscript it should be


What do you mean by session variable?

Javascript has global variables. You can access global variables from
any child frame or child window.

Javascript has session cookies. You can save small amount of data in a
cookie. I have had problems with session cookies in IE and do not use
session cookies any more. Session cookies are cookie that do not
include a time value. Instead, I set an expiration time on the cookies
I create.

I suggest using an existing set of cookie routines. The
document.cookie can contain existing cookies.



~kaeli~ has developed some cookie code. You may view it in this
article:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=ISO-8859-1&selm=MPG
..1af96c2a4f8abc94989dcc%40nntp.lucent.com

Also, ~kaeli~ reports that:
There's some even better code that sets more cookie properties like
domain, secure, path, etc here.
http://www.acm.uiuc.edu/webmonkeys/javascript/cookies.js



I wrote a short program for verifying cookies will work on a site.
The cookie code was taken from:
Functions from Using HTML 4, Java 1.1, and JavaScript 1.2:
Complete. Definitive. Invaluble.
by Eric Jim O'Donnell, et al.
and from the Netscape develper's site.

Robert

Here is my cookie testing
code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>Remember by cookie</title>


<script type="text/javascript">


// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}


function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}

function deleteCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function registerCookie(cookieName,cookieValue) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(cookieName, cookieValue, expires)
}
</script>

<body>
<p>Let's see if we can set then read a cookie


<script type="text/javascript">
document.write("when on (document.url) " + document.URL);
document.write("</p>");

var all = "I hope this cookie doesn't crumble.";

document.write("<p>");
document.write("document.cookie before deleteCookie = "
+ document.cookie + "<br><br>");
deleteCookie("mycookietry");
document.write("document.cookie after deleteCookie= "
+ document.cookie + "<br><br>");

registerCookie("mycookietry",all);

document.write("document.cookie after registerCookie = "
+ document.cookie + "<br><br>");

theMessage = getCookie("mycookietry");
document.write("theMessage = " + theMessage + "<br>");
document.write("</p>");
</SCRIPT>
</body>
</html>
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 12 Oct 2004 13:17:40, seen in
Robert said:
function registerCookie(cookieName,cookieValue) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(cookieName, cookieValue, expires)
}

function registerCookie(cookieName, cookieValue) {
var D = new Date()
D.setFullYear(D.getFullYear() + 1)
setCookie(cookieName, cookieValue, D)
}

makes more sense.

In the original code, expires = new Date(0) would be better; it's
generally worth spending a character to save determining a date/time
which will not be used.

It seems that in most books the code is not good.

In that context, the final D is used in D.toGMTString() - that format is
undefined in ECMA-262, and in my system does not comply with the
apparent specification for a cookie date string. But the interpretation
of cookie date strings is probably liberal enough, at least for the time
being.

It would have been smarter to follow the US FIPS for cookie dates; that
would have given AIUI all the advantages of ISO 8601 without being
embarrassingly foreign.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top