Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 1st, 2004, 01:10 AM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pigtail Send a message via Yahoo to pigtail
Default sharing a server-side variable with client-side

I have a hard time confirming if I CAN NOT have both server-side JavaScript and client-side JavaScript in the same html file. Does anyone know if I can have them in the same html file and if so, can my server-side Javascript shares a string variables with the client-side Javascript? How long can a string variable be?

I have written an ASP file that accesses databases and displayed data in a table format on the client’s browser. I tried to pass the constructed table back to the client so that the client-side Javascript section can use the innerHTML function to replace the entire table on the client’s browser without having the infamous flickering effect. I am having a hard time sharing my server-side variable which has the entire table with the client’s Javascript in the same file. Am I correct that the client-side Javascript can not retrieve server-side Session object, server-side Javascript variables and the Cookies for the session?





Reply With Quote
  #2 (permalink)  
Old April 1st, 2004, 03:36 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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.
Reply With Quote
  #3 (permalink)  
Old April 1st, 2004, 02:27 PM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to pigtail Send a message via Yahoo to pigtail
Default

You are wonderful!!!! All I was missing was the single quote around the Session variable in my client-side Javascript. Thanks A LOT!!! You have made my day!!

Reply With Quote
  #4 (permalink)  
Old April 1st, 2004, 02:38 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome. Glad it's working, although I may have given you a bit verbose answer for just a single quote.... ;)


Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #5 (permalink)  
Old October 26th, 2004, 06:41 AM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar, its great! but how can i do viceversa?
means to store javascript variable values in to a server variable without posting? eg:
if(confirm("Are u sure to delete"))
{
<% some asp code%>
}
else
{
<%again some asp code%>
}

is it possible? if yes how? can u help me?



Quote:
quote:Originally posted by Imar
 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.
Reply With Quote
  #6 (permalink)  
Old October 26th, 2004, 07:13 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't send client side data to the server without initiaiting an HTML request. There are a number of ways of doing this:

  •  
  • A tradional web form using "get" or "post"
     
  • Using msxml2.xmlhttp class or Mozilla equivalent, this may only be suitable in a trusted site/local intranet scenario
     
  • If you don't need any data returned and can use "get" method (data in querystring) then use
    Code:
    var sUrl = "myUrl/dbPage.asp?name=Joe&Age=42";
    Code:
    var oImg = new Image();
    oImg.src = sUrl;
    This will send the data to the asp page as if text elements named "Name" and "Age" contained "Joe" and 42.


--

Joe
Reply With Quote
  #7 (permalink)  
Old November 4th, 2004, 02:01 AM
Friend of Wrox
 
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii Pigtail
I have another approach to sharing a server-side variable with client-side

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
function NextQuotation() {

var obj = new ActiveXObject( "Microsoft.XMLHTTP" );
var url = "http://myurl/GetQuotation.asp"
obj.open( "POST",url, false );
 obj.send();
 nextquotation=obj.responseText
 alert(nextquotation);
}
</SCRIPT>
<form name="test">
<input type="text" name="quotation_id" >
<input type=button name="getQuote" onclick="NextQuotation()">
</form>


And my "GetQuotation.asp" ASP page is like
<% response.write session("counter") %>

Since u want to get session variables on the client side variable u can get through
 nextquotation in javascript function (i.e nextquotation=obj.responseText)



Hope ,It will help to find the solution.

Cheers :)



vinod
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Converting Client-side to Server-side Code kwilliams ASP.NET 2.0 Professional 1 November 21st, 2007 05:25 PM
Client-side variable feffe General .NET 0 May 9th, 2006 03:35 AM
Firing server side events at client side codes mehdi62b ASP.NET 1.0 and 1.1 Basics 6 May 18th, 2005 09:11 AM
Two Client Side vs Server Side issues Milo Classic ASP Professional 5 May 25th, 2004 02:47 PM
Accessing Server Side Data on Client Side steve456 Classic ASP Professional 3 October 15th, 2003 02:33 PM



All times are GMT -4. The time now is 11:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.