Click to See Complete Forum and Search --> : Examining my variable nulls it


DanWalter
06-09-2006, 05:18 PM
Hello:

There must be something I don't understand about the timing or order of parsing the javascript code and merging it back with the static HTML. I have a variable that is declared and filled in the server side code, before the HTML declarations begin. A little while down the HTML page, I have a call to server side code. After this code runs, the variable is now null!

Here is my sample code and output.

Code (in HTML page - the variable strComment1 is declared and filled by a database lookup in server side code.):

<%Response.Write("The comment is :" + strComment1 + ": and now it is :" + strComment1 +":");%>

Output:
The comment is :this is a comment: and now it is :null:

Heisenberg's uncertainty principle states that observing a phenomenon changes the phenomenon, but this is ridiculous! I have heard of these things referred to as a "Heisenbug" in programming - a bug that changes when you test it.

I can work around my specific problem - I just want to understand why this happens.

TIA,
Dan

lmf232s
06-09-2006, 05:56 PM
DanWalter,
Would need to see some more code to be able to help you.
Its most likely something very simple that your just overlooking
which is causing the variable to be reset.

Are you sure that there is a value being returned from the DB?

DanWalter
06-09-2006, 06:06 PM
Imf232s:

There is data in the variable the first time it is referenced, then it is null the second time.

Here is some code:

Broken code:
Server side declares strComment1 and fills it from SQL database.

HTML code:
<img src="GRAPHICS/comment3.gif" border="<% if (!(strComment1 == "")) {%>1<%}else{%>0<%}%>" align="right" width="65" height="15">
<input type="hidden" name="Comment1" value="<%=strComment1%>">

This was compiled as
<img src="GRAPHICS/comment3.gif" border="1" align="right" width="65" height="15">
<input type="hidden" name="Comment1" value="">

Note that the border value of 1 indicates something other than nothing in the variable strComment1. In fact, strComment1 is "this is a comment".


Fixed code:
Server side declares strComment1 and fills it from SQL database.
Server side initializes strBorder to 0, then makes it 1 if there are contents in strComment1.

HTML code:
<img src="GRAPHICS/comment3.gif" border="<%=strBorder%>" align="right" width="65" height="15">
<input type="hidden" name="Comment1" value="<%=strComment1%>">

This is compiled as:
<img src="GRAPHICS/comment3.gif" border="1" align="right" width="65" height="15">
<input type="hidden" name="Comment1" value="this is a comment">

Any ideas?

Dan

russell
06-09-2006, 06:32 PM
need to see the server-side code that u didn't show us. the server side code is writing a javascript variable? if not, the js can't see your server-side var.

Example
----- server ---
strComment1 = "Hello World"
** js can't see it

----- server ---
Response.Write "<script>" & vbCrLf
Response.Write "var strComment1 = 'hello world'" & vbCrLf
** js CAN see it

DanWalter
06-13-2006, 04:28 PM
Russell:

What stumps me is this simple example:
On the server side
var strComment1 = "this is a comment";

Now, on the client side, in HTML, we have this call to a server parse:
<%Response.Write("The comment is :" + strComment1 + ": and now it is :" + strComment1 +":");%>

I would expect the output to be:
The comment is :this is a comment: and now it is :this is a comment:

Instead, what I get is this:
The comment is :this is a comment: and now it is :null:

Why is the second reference to strComment null, especially when the whole server side call is done in one line?

Dan

russell
06-13-2006, 05:02 PM
are you sure that you've (1) declared it in the proper scope and (2) didn't make any typos in the variable name?

<%@Language=JScript%>
<%
var strComment1;
strComment1 = "This is a Comment"

Response.Write("The comment is :" + strComment1 + ": and now it is :" + strComment1 +":");
%>

Here's the output I getThe comment is :This is a Comment: and now it is :This is a Comment:

russell
06-13-2006, 05:12 PM
Now, on the client side, in HTML, we have this call to a server parse:
<%Response.Write("The comment is :" + strComment1 + ": and now it is :" + strComment1 +":");%>
I'm betting you have something out of order. You can't call a server side variable from the client side. It only looks that way from the inline Server-Side script -- which by the way is a poor (but quite common, sigh) practice. If you post the entire script, we'll show ya what's missing...

lmf232s
06-13-2006, 05:36 PM
and to go along w/ what russell mentioned about the typo of the variable.

Add <%Option Explicit%> to the top of you page if you have not all ready dont it. It will require you to declare all your variables (Good practice) and may just reveal the source of your problem.

russell
06-13-2006, 07:03 PM
agreed. good call lmf