473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Server side variable within a JS file

Dan
Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');

and my aspx page contains:-

<script type="text/javascript" src="test.js">

This simple pops up an alert box containing <%=tmpVar%>, rather than
the actual value of my server side variable.

I can understand why it doesn't parse it to my test.js file.

Anyone got any suggestions on how i can do this? I don't really want to
have to include my javascript in my aspx header, due to maintenance
issues.

Thanks in advance for any suggestions.

Dan

Jan 5 '07 #1
6 5013
Dan wrote:
For example, my test.js file contains

alert('<%=tmpVar%>');

and my aspx page contains:-

<script type="text/javascript" src="test.js">

This simple pops up an alert box containing <%=tmpVar%>, rather than
the actual value of my server side variable.

I can understand why it doesn't parse it to my test.js file.
You would need to make sure your server preprocesses the file e.g.
<script type="text/javascript" src="file.asp"></script>
meaning you have asp on the server that dynamically generates
client-side JavaScript code.
A static .js file is just that, a static file, the server will simply
pass it on and your ASP stuff does not get processed at all.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 5 '07 #2

"Dan" <da**********@newcross-nursing.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');
If you take the quotes away, it will probably work like you want.
The tag <%=tmpVar%returns a string, so it "is already" a string - putting
the expression in quotes make the string "not asp" but a literal string.
in other words, make a minor modification like so:
alert(<%tmpVar%>); // **(leave out the quotes)**

Jan 6 '07 #3

"Hal Rosser" <hm******@bellsouth.netwrote in message
news:Xa*******************@bignews7.bellsouth.net. ..
>
"Dan" <da**********@newcross-nursing.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
>Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');
If you take the quotes away, it will probably work like you want.
The tag <%=tmpVar%returns a string, so it "is already" a string -
putting the expression in quotes make the string "not asp" but a literal
string.
in other words, make a minor modification like so:
alert(<%tmpVar%>); // **(leave out the quotes)**
OOPS - I told ya wrong - didn't notice the code was in a "js" file.
The code needs to be in your asp file to get processed by the server.
Jan 6 '07 #4
"Dan" <da**********@newcross-nursing.comwrote in
news:11**********************@11g2000cwr.googlegro ups.com:
Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');

and my aspx page contains:-

<script type="text/javascript" src="test.js">

This simple pops up an alert box containing <%=tmpVar%>, rather than
the actual value of my server side variable.

I can understand why it doesn't parse it to my test.js file.

Anyone got any suggestions on how i can do this? I don't really want to
have to include my javascript in my aspx header, due to maintenance
issues.

Thanks in advance for any suggestions.

Dan

Have your serverside program write js code to initialize tmpVar
'litteraly' on your page.

My VB is a bit rusty, so the code below should be considered pseudocode:
dim tmpVar
tmpVar=666
....
Response.Write "<html><head>"
Response.Write "<script type='text/javascript'>"
Response.Write "var tmpVar=" & tmpVar
Response.Write "</script>
Response.Write "<script type="text/javascript" src='test.js'/>"
Response.Write "</head>"
....

Here we've created a js global variable tmpVar you can now reference in
your js file like so :

alert(tmpVar);
regards

Ward
Jan 6 '07 #5
VK

Dan wrote:
Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');

and my aspx page contains:-

<script type="text/javascript" src="test.js">

This simple pops up an alert box containing <%=tmpVar%>, rather than
the actual value of my server side variable.

I can understand why it doesn't parse it to my test.js file.

Anyone got any suggestions on how i can do this? I don't really want to
have to include my javascript in my aspx header, due to maintenance
issues.

Thanks in advance for any suggestions.
You may use the fall-back segment of the <scriptelement for that. A
<scriptelement with src attribute set consists of two blocks:
1) the source file pointed by src
2) fall-back code between <scripttags for UAs without src support.

If src attribute is set is supported then anything between <scripttag
is automatically ignored but still available at run-time as
document.scripts[i].text

Because for many years already there is not a single UA w/o script src
support, the fall-back part left empty:
<script src="source.js"></script>
or used for some other purposes, say for copyright info:

<script src="source.js">
Copyright 2006 Acme, Inc.
</script>

Another use is exactly for your case: to provide session values for a
static library:

<script src="source.js">
{data1:"value1", data2:"value2"}
</script>

so on ASP/PHP it would be:

<script src="source.js">
<processing instructions>
</script>

Of course JSON format is much more convenient here, then the run-time
task as simple as:

// presuming this is the first script element on the page:

var params = eval(document.script[0].text);

Jan 7 '07 #6
Dan wrote on 05 jan 2007 in comp.lang.javascript:
Excuse me if i'm being a bit thick here, but is it possible to
reference a server side variable within an embedded js source file.

For example, my test.js file contains

alert('<%=tmpVar%>');

and my aspx page contains:-

<script type="text/javascript" src="test.js">

This simple pops up an alert box containing <%=tmpVar%>, rather than
the actual value of my server side variable.
Use:

<script type="text/javascript" src="testjs.asp">

containing perhaps a session variable:

alert('<%=session("tmpVar")%>')

[classic asp solution, feel free to port it to the unknown (to me) asp.net]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 7 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: JT | last post by:
im trying to use the following code to log whenever a user clicks through this particlular message box - however, this currently logs regardless of whether or not the message box was clicked - im...
2
by: Dicky Cheng | last post by:
Hi, I am using .net remoting technology. I set up a .net remoting client and server in IIS. When the client calls the server, the server will run a long duration method (30-60seconds). I have a...
6
by: ccg | last post by:
I am trying to print a file from an ASP script but haven't been able to find any way of making this happen. I have an EPL (Eltron Thermal Printer language) file that is being generated from the...
6
by: Don | last post by:
I'm trying to come up with a way within a client-side web page of uploading a couple files to a server-side PHP program without using a <form...>. I don't want to give up the page which happens...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
4
by: Bob T | last post by:
Hi All, I am trying to pass a variable from my VB asp.net script (from for example Sub Page_Load in mypage.aspx.vb) to my Client side script. I have found and looked at a very good example...
8
by: Mike Fellows | last post by:
Ok, im not sure if this is at all possible and if it is how i go about it is beyond me i have a piece of client side code that requires a piece of data from the server side (an ID number in this...
5
by: Casey | last post by:
Hello, Can someone give me specific code to replace text on a page using server side javascript? I need to use server-side because I need the output to be recognized in the final HTML so that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.