Hi there,
That's a lot of questions in one question... Let me try to tackle them all.
First of all, you *can* share variables between server side and client side code, but not directly. It's important to understand how it works under the hood if you want to share the values of these two kind of variables.
A Server side variable exists on the server, while a JavaScript variable lives on the client. This means that you cannot directly alter one variable from the other location. However, you can use the standard Web mechanisms like Response and POST to send the
values of those variables from the server to the client and back.
Let me clarify this with an example. The following piece of code will alert the value of a Session variable:
Code:
<%
Session("MyTest") = Now() ' store date and time in session var
%>
<script type="text/javascript">
var whatTimeIsIt = '<%=Session("MyTest")%>';
alert(whatTimeIsIt);
</script>
This is a trivial, but important example. What happens is that when the page loads at the server, the current date and time is stored in a Session variable. Then the page continues and reaches the client side JavaScript block which it will send to the browser. However, it also encounters a
server side code block (<%= and %>) which injects the value of the session variable into the client side code.
If you look at the source of the page in the browser, you'll see something like this:
Code:
<script type="text/javascript">
var whatTimeIsIt = '4/1/2004 9:32:06 AM';
</script>
So, as you can see, you can pass the value of the session variable into a local variable.
The string in JavaScript can be (
AFAIK) virtually endless. I am not aware of any limitation. However, you should be aware of some encoding issues, for example::
Code:
<%
Session("MyTest") = "A variable's value"
%>
<script type="text/javascript">
var whatValue = '<%=Session("MyTest")%>';
</script>
This will cause a problem because the MyTest variable contains an apostrophe. The code in the browser will end up like this:
var whatValue = 'A variable's value';
As you can see, the value is cut of after the word variable, as the ' is seen as the end of the string.
Therefor it's important to escape any output you're trying to send to the browser. You can use Escaping methods or replace the illegal characters yourself.
If you need to send the client side value back to the server, you'll need to add it to a form somehow. How you do this is up to you: you can write out the server side variables to hidden or visible fields, allow the user to change them (or not), have the client recalculate them, whatever your application requires. At the end, the variables must be added to the form, and submitted back to the server. There, the same principle applies again, but in reverse:
Code:
<%
' Assume form is submitted
Session("MyTest") = Request.Form("txtHiddenMyTest")
%>
This saves the value of the hidden form field back in the server side session variable.
If you want, you can access cookies from the client as well. There are widely available JavaScript libraries / functions that allow you to work with cookies from within client side JavaScript.
I think this answers all your question you asked here. If not, let me know.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.