Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Server side variable within a JS file (http://www.velocityreviews.com/forums/t929078-server-side-variable-within-a-js-file.html)

Dan 01-05-2007 05:26 PM

Server side variable within a JS file
 
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


Martin Honnen 01-05-2007 05:35 PM

Re: Server side variable within a JS file
 
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/

Hal Rosser 01-06-2007 05:10 AM

Re: Server side variable within a JS file
 

"Dan" <dan_williams@newcross-nursing.com> wrote in message
news:1168018004.761535.174010@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)**




Hal Rosser 01-06-2007 05:18 AM

Re: Server side variable within a JS file
 

"Hal Rosser" <hmrosser@bellsouth.net> wrote in message
news:XaGnh.20992$cB6.20874@bignews7.bellsouth.net. ..
>
> "Dan" <dan_williams@newcross-nursing.com> wrote in message
> news:1168018004.761535.174010@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.



King Albert 01-06-2007 05:59 PM

Re: Server side variable within a JS file
 
"Dan" <dan_williams@newcross-nursing.com> wrote in
news:1168018004.761535.174010@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

VK 01-07-2007 06:32 PM

Re: Server side variable within a JS file
 

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 <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[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);


Evertjan. 01-07-2007 07:24 PM

Re: Server side variable within a JS file
 
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)

bhaskarareddy 06-11-2012 02:26 PM

We can pass the serverside value is as below syntax
<%=variblevalue%>

for more details refer this link

Server side variable to js


All times are GMT. The time now is 06:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.