473,804 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('<%=tmpVa r%>');

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 5073
Dan wrote:
For example, my test.js file contains

alert('<%=tmpVa r%>');

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**********@n ewcross-nursing.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.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('<%=tmpVa r%>');
If you take the quotes away, it will probably work like you want.
The tag <%=tmpVar%retur ns 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******@bells outh.netwrote in message
news:Xa******** ***********@big news7.bellsouth .net...
>
"Dan" <da**********@n ewcross-nursing.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.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('<%=tmpV ar%>');
If you take the quotes away, it will probably work like you want.
The tag <%=tmpVar%retur ns 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**********@n ewcross-nursing.comwrot e in
news:11******** **************@ 11g2000cwr.goog legroups.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('<%=tmpVa r%>');

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><hea d>"
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('<%=tmpVa r%>');

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 <scriptelemen t for that. A
<scriptelemen t 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.script s[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.s cript[0].text);

Jan 7 '07 #6
Dan wrote on 05 jan 2007 in comp.lang.javas cript:
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('<%=tmpVa r%>');

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('<%=sessi on("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
1749
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 assuming this is because the server-side code can't see the client side if condition. but how can i set the varMsgBox variable as a server-side variable? <!--switch to client-side VBScript to use the MsgBox--> <script language="VBSCRIPT"> dim...
2
3945
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 test on it that if the network broken at the time the client have already send the remoting request and waiting for the server, the client side will wait infinitely by default, even if i already set the executionTimeout to 90seconds in...
6
3690
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 script and I want a way to print this file. An EPL file is basically printed the same way as a postscript PRN would be printed. From a command prompt I can type: copy label.EPL LPT1
6
1745
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 after the <form...> executes. Instead, I want to upload the files, then execute a <form...> for another purpose. Any help would be appreciated. Thanks, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----...
5
3614
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 instruction document (not data based) - same as using an xml web control. The resulting html is on the client? but what about the server side of things? Trying to figure out how to change and save the xmlDocument. It I put a button OUTSIDE of the...
4
3512
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 "Client and Server Scripting in Web Pages" but it only shows server side scripting that is written in HTML in mypage.aspx and not script from mypage.aspx.vb. How can I pass a variable value from mypage.aspx.vp to my client side script?
8
3688
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 case) how can i pass the data from one side to the other, or retrieve it from the webform on the client side? i hope ive explained this properly
5
4789
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 google can index it. Here is a specific example of what I want to do: <div id=SomeText> Here is some text. I went to the baseball game </div>
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.