469,365 Members | 1,749 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,365 developers. It's quick & easy.

Using ASP Variables

I use ASP to obtain data from a database and I have a piece of javascript
code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]

Please help or point me to a website with this solution. Thanks much for
your help.

Craig

w1****@hotSPAMmail.com (remove SPAM before sending)
Jul 20 '05 #1
16 10988
Craig L wrote:
I use ASP to obtain data from a database and I have a piece of javascript
code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]


You can't access the ASP variables directly, because ASP is done on the
server, and Javascript is on the client (web browser). By the time the
Javascript is "executing" the ASP has already finished.

You can try this though :-

Items[1]=["First Name", "<%=strFirstName%>",""]
Items[2]=["Last Name", "<%=strLastName%>",""]

HTH!

bengee

Jul 20 '05 #2
I've done something like the following to get ASP variables into javascript

<%
strFirstName = (whatever you want it to equal)
strLastName = (whatever you want it to equal)

Response.Write "<script language=JavaScript>"
Response.Write "var sFirstName=" & strFirstName
Response.Write "var sLastName=" & strLastName
Response.Write "</script>"

Now, the variables sFirstName and sLastName are available to your JavaScript

What you have done, is written a <script> block via ASP that gives you
JavaScript variables....

Mike

"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:w8********************@comcast.com...
I use ASP to obtain data from a database and I have a piece of javascript
code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]

Please help or point me to a website with this solution. Thanks much for
your help.

Craig

w1****@hotSPAMmail.com (remove SPAM before sending)

Jul 20 '05 #3
Bengee:
Thanks for the help. the <%= info didn't help. Do you know if the javascript
code can use it if I create a session object for the variable? I tried some
of this with no luck.

Craig

"bengee" <po********@localhost.localdomain> wrote in message
news:L8******************@wards.force9.net...
Craig L wrote:
I use ASP to obtain data from a database and I have a piece of javascript code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]


You can't access the ASP variables directly, because ASP is done on the
server, and Javascript is on the client (web browser). By the time the
Javascript is "executing" the ASP has already finished.

You can try this though :-

Items[1]=["First Name", "<%=strFirstName%>",""]
Items[2]=["Last Name", "<%=strLastName%>",""]

HTH!

bengee

Jul 20 '05 #4
Craig L wrote:
Bengee:
Thanks for the help. the <%= info didn't help. Do you know if the javascript
code can use it if I create a session object for the variable? I tried some
of this with no luck.


There's no way Javascript can access an ASP variable. However,
Javascript is the "output" from the ASP code, so you can set a
Javascript variable to be that of an ASP variable. Once this is set
though and being viewed in the browser, you can't change it server-side
without some sort of re-sumbit mechanism.

Have a look a Mike's example below.

bengee

Jul 20 '05 #5
Bengee:
I'm trying what Mike mentioned since it really makes sense; getting ASP to
generate the javascript code, but it doesn't work for me yet. I have the
response.buffer=True set. Not sure what I'm doing wrong.

Craig

"bengee" <po********@localhost.localdomain> wrote in message
news:tH******************@wards.force9.net...
Craig L wrote:
Bengee:
Thanks for the help. the <%= info didn't help. Do you know if the javascript code can use it if I create a session object for the variable? I tried some of this with no luck.


There's no way Javascript can access an ASP variable. However,
Javascript is the "output" from the ASP code, so you can set a
Javascript variable to be that of an ASP variable. Once this is set
though and being viewed in the browser, you can't change it server-side
without some sort of re-sumbit mechanism.

Have a look a Mike's example below.

bengee

Jul 20 '05 #6
Craig L wrote:
Bengee:
I'm trying what Mike mentioned since it really makes sense; getting ASP to
generate the javascript code, but it doesn't work for me yet. I have the
response.buffer=True set. Not sure what I'm doing wrong.


Post some code here then and let us have a look.
Jul 20 '05 #7
Here is my exact code. This page receives a URL as the following
"NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"

In ASP, a reference to Request("TBLS") will return the string
"AttMarket,ConfComment"

I take that info and save it into a JavaScript array that I can access
throughout the page on the client side...
<preliminary HTML stuff>
<body onload="Loaded()">
<%

'-- A bunch of code that means nothing as far as your question goes...

'-- Throw out some JavaScript make it easy to get to the Request
parameters.
Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34) & ">"
Response.Write "numTables=" & Request("TBLS").Count & ";"
Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1) &
");"
For i = 1 to Request("TBLS").Count
Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
Request("TBLS")(i) & chr(34) & ";"
Next
Response.Write "</script>"

'-- More code that means nothing

%>
</body>

This is the code generated (from View->Source)

<body onload="Loaded()">
<script Language="JavaScript">numTables=2;allTables = new
Array(1);allTables[0] = "AttMarket";allTables[1] = "ConfComment";</script>
<more html stuff>
</body>

Mike

"bengee" <po********@localhost.localdomain> wrote in message
news:k8******************@wards.force9.net...
Craig L wrote:
Bengee:
I'm trying what Mike mentioned since it really makes sense; getting ASP to generate the javascript code, but it doesn't work for me yet. I have the
response.buffer=True set. Not sure what I'm doing wrong.


Post some code here then and let us have a look.

Jul 20 '05 #8
On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
<mb***************@skypoint.com> wrote:
| Here is my exact code. This page receives a URL as the following
| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
|
| In ASP, a reference to Request("TBLS") will return the string
| "AttMarket,ConfComment"
|
| I take that info and save it into a JavaScript array that I can access
| throughout the page on the client side...
| <preliminary HTML stuff>
| <body onload="Loaded()">
| <%
|
| '-- A bunch of code that means nothing as far as your question goes...
|
| '-- Throw out some JavaScript make it easy to get to the Request
| parameters.
| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34) & ">"
| Response.Write "numTables=" & Request("TBLS").Count & ";"
| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1) &
| ");"
| For i = 1 to Request("TBLS").Count
| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
| Request("TBLS")(i) & chr(34) & ";"
| Next
| Response.Write "</script>"
|
| '-- More code that means nothing
|
| %>
| </body>


You are trying to get the javascript to read from the database. This
will not work. You need to do this server-side.

Have a look at
http://www.asp101.com/samples/viewas...db%5Fdsn%2Easp
---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Jul 20 '05 #9
YES, it will work, and it DOES work....

"Jeff North" <jn****@yourpantsbigpond.net.au> wrote in message
news:bg********************************@4ax.com...
On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
<mb***************@skypoint.com> wrote:
| Here is my exact code. This page receives a URL as the following
| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
|
| In ASP, a reference to Request("TBLS") will return the string
| "AttMarket,ConfComment"
|
| I take that info and save it into a JavaScript array that I can access
| throughout the page on the client side...
| <preliminary HTML stuff>
| <body onload="Loaded()">
| <%
|
| '-- A bunch of code that means nothing as far as your question goes...
|
| '-- Throw out some JavaScript make it easy to get to the Request
| parameters.
| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34) & ">"| Response.Write "numTables=" & Request("TBLS").Count & ";"
| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1) &| ");"
| For i = 1 to Request("TBLS").Count
| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
| Request("TBLS")(i) & chr(34) & ";"
| Next
| Response.Write "</script>"
|
| '-- More code that means nothing
|
| %>
| </body>


You are trying to get the javascript to read from the database. This
will not work. You need to do this server-side.

Have a look at
http://www.asp101.com/samples/viewas...db%5Fdsn%2Easp
---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------

Jul 20 '05 #10
Craig L wrote:
I use ASP to obtain data from a database and I have a piece of javascript
code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]

Please help or point me to a website with this solution. Thanks much for
your help.

Craig

w1****@hotSPAMmail.com (remove SPAM before sending)


Trivial:

Items[1]=["First Name", "<%= session('FirstName') %>",""];
Items[2]=["Last Name", "<%= session('LastName') %>",""];

If <%= %> doesn't evaluate properly inside double-quotes (I'm not sure how
ASP deals with this), then you could use something like:

Items[1]=["First Name", <%= dquote(session("FirstName")) %>,""];
Items[2]=["Last Name", <%= dquote(session("LastName")) %>,""];

(where dquote() is a little function that you provide that wraps the passed
string in double-quotes)

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #11
On Wed, 5 Nov 2003 08:50:38 -0600, in comp.lang.javascript "Mike"
<mb***************@skypoint.com> wrote:
| YES, it will work, and it DOES work....
Sorry, I should've replied to Graig L posting.
But my point still stands. You can not use client-side javascript to
read data directly from a database.
| "Jeff North" <jn****@yourpantsbigpond.net.au> wrote in message
| news:bg********************************@4ax.com...
| > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
| > <mb***************@skypoint.com> wrote:
| >
| > >| Here is my exact code. This page receives a URL as the following
| > >| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
| > >|
| > >| In ASP, a reference to Request("TBLS") will return the string
| > >| "AttMarket,ConfComment"
| > >|
| > >| I take that info and save it into a JavaScript array that I can access
| > >| throughout the page on the client side...
| > >| <preliminary HTML stuff>
| > >| <body onload="Loaded()">
| > >| <%
| > >|
| > >| '-- A bunch of code that means nothing as far as your question goes...
| > >|
| > >| '-- Throw out some JavaScript make it easy to get to the Request
| > >| parameters.
| > >| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34)
| & ">"
| > >| Response.Write "numTables=" & Request("TBLS").Count & ";"
| > >| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1)
| &
| > >| ");"
| > >| For i = 1 to Request("TBLS").Count
| > >| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
| > >| Request("TBLS")(i) & chr(34) & ";"
| > >| Next
| > >| Response.Write "</script>"
| > >|
| > >| '-- More code that means nothing
| > >|
| > >| %>
| > >| </body>
| >
| > You are trying to get the javascript to read from the database. This
| > will not work. You need to do this server-side.
| >
| > Have a look at
| > http://www.asp101.com/samples/viewas...db%5Fdsn%2Easp
| >
| >
| > ---------------------------------------------------------------
| > jn****@yourpantsbigpond.net.au : Remove your pants to reply
| > ---------------------------------------------------------------
|


---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Jul 20 '05 #12
Hey Gang:
I'll try what Jeff said to do as soon as I get a chance - probably this
afternoon. Will let you know and post the working code for others.

Craig

"Jeff North" <jn****@yourpantsbigpond.net.au> wrote in message
news:24********************************@4ax.com...
On Wed, 5 Nov 2003 08:50:38 -0600, in comp.lang.javascript "Mike"
<mb***************@skypoint.com> wrote:
| YES, it will work, and it DOES work....


Sorry, I should've replied to Graig L posting.
But my point still stands. You can not use client-side javascript to
read data directly from a database.
| "Jeff North" <jn****@yourpantsbigpond.net.au> wrote in message
| news:bg********************************@4ax.com...
| > On Tue, 4 Nov 2003 16:04:04 -0600, in comp.lang.javascript "Mike"
| > <mb***************@skypoint.com> wrote:
| >
| > >| Here is my exact code. This page receives a URL as the following
| > >| "NewPage.asp?TBLS=AttMarket&TBLS=ConfComment"
| > >|
| > >| In ASP, a reference to Request("TBLS") will return the string
| > >| "AttMarket,ConfComment"
| > >|
| > >| I take that info and save it into a JavaScript array that I can access| > >| throughout the page on the client side...
| > >| <preliminary HTML stuff>
| > >| <body onload="Loaded()">
| > >| <%
| > >|
| > >| '-- A bunch of code that means nothing as far as your question goes...| > >|
| > >| '-- Throw out some JavaScript make it easy to get to the Request
| > >| parameters.
| > >| Response.Write "<script Language=" & chr(34) & "JavaScript" & chr(34)| & ">"
| > >| Response.Write "numTables=" & Request("TBLS").Count & ";"
| > >| Response.Write "allTables = new Array(" & (Request("TBLS").Count - 1)| &
| > >| ");"
| > >| For i = 1 to Request("TBLS").Count
| > >| Response.Write "allTables[" & (i - 1) & "] = " & chr(34) &
| > >| Request("TBLS")(i) & chr(34) & ";"
| > >| Next
| > >| Response.Write "</script>"
| > >|
| > >| '-- More code that means nothing
| > >|
| > >| %>
| > >| </body>
| >
| > You are trying to get the javascript to read from the database. This
| > will not work. You need to do this server-side.
| >
| > Have a look at
| > http://www.asp101.com/samples/viewas...db%5Fdsn%2Easp
| >
| >
| > ---------------------------------------------------------------
| > jn****@yourpantsbigpond.net.au : Remove your pants to reply
| > ---------------------------------------------------------------
|


---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------

Jul 20 '05 #13
I think I can get this to a simplier problem now that I have played with it.
Here is what is happening. This code works for what I'm trying to do:

<SCRIPT language=JavaScript>

var strFirstName="Don"

var strLastName="Rose"

</SCRIPT>

<SCRIPT>

Items[0]=[strFirstName, "", ""];

Items[1]=[strLastName, "", ""];

</SCRIPT>

This code getting ASP to write the javascript equivalent like this, doesn't
work:

<%

response.write "<SCRIPT Language=" & chr(34) & "JavaScript" & chr(34)

response.write "var strFirstName=" & chr(34) & "Don" & chr(34)

response.write "var strLastName=" & chr(34) & "Rose" & chr(34)

response.write "</SCRIPT>"

%>

<SCRIPT>

Items[0]=[strFirstName, "", ""];

Items[1]=[strLastName, "", ""];

</SCRIPT>

If I can just get this right, I can get the rest working on my own.

Craig

"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:3F***************@agricoreunited.com...
Craig L wrote:
I use ASP to obtain data from a database and I have a piece of javascript code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]

Please help or point me to a website with this solution. Thanks much for
your help.

Craig

w1****@hotSPAMmail.com (remove SPAM before sending)
Trivial:

Items[1]=["First Name", "<%= session('FirstName') %>",""];
Items[2]=["Last Name", "<%= session('LastName') %>",""];

If <%= %> doesn't evaluate properly inside double-quotes (I'm not sure how
ASP deals with this), then you could use something like:

Items[1]=["First Name", <%= dquote(session("FirstName")) %>,""];
Items[2]=["Last Name", <%= dquote(session("LastName")) %>,""];

(where dquote() is a little function that you provide that wraps the

passed string in double-quotes)

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 20 '05 #14
Visit the posting "ANS: Using ASP Variables" posted on 11/09/03 for the
solution to this issue.

Craig

w1******@hotSPAMmail.com (remove SPAM before emailing).
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:w8********************@comcast.com...
I use ASP to obtain data from a database and I have a piece of javascript
code I use on my website. I want to use the database variables in my
javascript. I'm not very familiar with javascript, but here is some info
similar to what I'm trying to do.

<%
strFirstName=session("FirstName") ASP code
strLastName=session("LastName")
%>

The javascript code I want to use the variables in.
Items[1]=["First Name", strFirstName,""]
Items[2]=["Last Name", strLastName,""]

Please help or point me to a website with this solution. Thanks much for
your help.

Craig

w1****@hotSPAMmail.com (remove SPAM before sending)

Jul 20 '05 #15
Jeff North wrote:
You can not use client-side javascript to read data directly from
a database.


Depends. There is no native support but an additional API using HTTP
requests could provide access to database on the server and another
one could provide that for a database on the client.
PointedEars
Jul 20 '05 #16
On Mon, 24 Nov 2003 23:04:27 +0100, Thomas 'PointedEars' Lahn wrote:
Jeff North wrote:
You can not use client-side javascript to read data directly from a
database.


Depends. There is no native support but an additional API using HTTP
requests could provide access to database on the server and another one
could provide that for a database on the client.


Uh, you still wouldn't be reading the database with JavaScript then would
you?

--
i.m.
The USA Patriot Act is the most unpatriotic act in American history.

Jul 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Guy Robinson | last post: by
13 posts views Thread by Steven Scaife | last post: by
17 posts views Thread by Davݭ ١risson | last post: by
19 posts views Thread by Mitesh | last post: by
reply views Thread by Luis Zarrabeitia | last post: by
15 posts views Thread by r0g | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.