Server side variable within a JS file


D

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
 
Ad

Advertisements

M

Martin Honnen

Dan said:
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.
 
H

Hal Rosser

Dan said:
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)**
 
H

Hal Rosser

Hal Rosser said:
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.
 
K

King Albert

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
 
V

VK

Dan said:
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 <script> element for that. A
<script> element with src attribute set consists of two blocks:
1) the source file pointed by src
2) fall-back code between <script> tags for UAs without src support.

If src attribute is set is supported then anything between <script> tag
is automatically ignored but still available at run-time as
document.scripts.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);
 
Ad

Advertisements

E

Evertjan.

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]
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top