473,661 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

server side global variable failure

Can anyone tell me why this doesn't work?

My end goal here is to be able to write Javascript library modules that
can be used by both client and server side code. My test here consists
of 2 files:

testGlobal.asp :

<%@LANGUAGE="JA VASCRIPT" CODEPAGE="1252" %>
<script language="JavaS cript" type="text/javascript" runat="server"
src="testGlobal .js"></script>
<%
Response.Write( "Global value=" + testGlobal());
%>

testGlobal.js :

var globalVal = "5";

function testGlobal() {

return globalVal;
} /* end of testGlobal */

My problem is that when I call testGlobal() from the server side,
globalVal is undefined. If I move it inside the function, all is well.

Why can't testGlobal() see the variable globalVal ?

Thanks,

Jan 20 '06 #1
6 1951
"Bruce" <ba********@yah oo.ca> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Can anyone tell me why this doesn't work?

My end goal here is to be able to write Javascript library modules that
can be used by both client and server side code. My test here consists
of 2 files:

testGlobal.asp :

<%@LANGUAGE="JA VASCRIPT" CODEPAGE="1252" %>
<script language="JavaS cript" type="text/javascript" runat="server"
src="testGlobal .js"></script>
<%
Response.Write( "Global value=" + testGlobal());
%>

testGlobal.js :

var globalVal = "5";

function testGlobal() {

return globalVal;
} /* end of testGlobal */

My problem is that when I call testGlobal() from the server side,
globalVal is undefined. If I move it inside the function, all is well.

Why can't testGlobal() see the variable globalVal ?

Thanks,


The following works:

[testGlobal.js follows]

<%
// testGlobal.js
var globalVal = "5";
function testGlobal() {
return globalVal;
}
%>

[testGlobal.asp follows]

<%@ LANGUAGE="JAVAS CRIPT" CODEPAGE="1252" %>
<!--#include file="test~Glob al.js"-->
<% Response.Write( "Global value=" + testGlobal()); %>
But it's not what you want because the ".js" file has <% and %>.
Is this correct?
Jan 20 '06 #2


Bruce wrote:

My problem is that when I call testGlobal() from the server side,
globalVal is undefined. If I move it inside the function, all is well.

Why can't testGlobal() see the variable globalVal ?


That is some unfortunate oddity in classic ASP, there might be an old
article on msdn.microsoft. com that explains that better but mainly
<script runat="server" src="file.js"></script>
is only useful for declaring functions to be used then in <% %>.

It is a pain for instance if you wanted to do prototype based
inheritance as if you have e.g.
function F () {}
F.prototype.met hodName = function () {}
in a <script runat="server"> then later on you can use the function F in
<% %> blocks as the function declaration is being processed but the
assignment to the prototype is either not processed at all or I think
only after your <% %> blocks meaning if you do
<%
var f = new F();
you don't have the method available.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 20 '06 #3
> But it's not what you want because the ".js" file has <% and %>. Is this correct?

Right. I can still call the function if I exclude "<%" and "%>", but
for a reason I don't understand the function can't access the variable.

If I use "<%" and "%>" then I can't use the .js file on the client side
and I want to use it on both the client and server.

Jan 20 '06 #4
"Bruce" <ba********@yah oo.ca> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
But it's not what you want because the ".js" file has <% and %>. Is this
correct?
Right. I can still call the function if I exclude "<%" and "%>", but
for a reason I don't understand the function can't access the variable.

If I use "<%" and "%>" then I can't use the .js file on the client side
and I want to use it on both the client and server.


The following works almost the way (I think) you want:

[testGlobal.js follows]

//<%
// testGlobal.js
var globalVal = "5";
function testGlobal() {
return globalVal;
}
//%>

[testGlobal.asp follows]

<%@ LANGUAGE="JAVAS CRIPT" CODEPAGE="1252" %>
<!--#include file="testGloba l.js"-->
<% Response.Write( "<br>Global value=" + testGlobal()); %>
<html>
<head>
<script type="text/javascript" src="testGlobal .js"></script>
</head>
<body onload="alert(' Global value=' + testGlobal())">
</body>
</html>
It shows the execution of the function servier-side and client-side.

However, the server-size execution outputs the unwanted "//".

Perhaps it will give you some ideas... Good luck.
Jan 20 '06 #5
Yes, it works exactly the way I want except for the display of "//" as
you pointed out. Any ideas on how to get rid of that?

Jan 20 '06 #6
"Bruce" <ba********@yah oo.ca> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Yes, it works exactly the way I want except for the display of "//" as
you pointed out. Any ideas on how to get rid of that?


Change the background to black then you can't see it. :-)

Change it to something you want to see; for example,

// <b>Hello World</b> //<%

You might want to open a new post about just this.
Jan 20 '06 #7

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

Similar topics

1
5895
by: Chris | last post by:
Background: I am using a MS Access 2000 front end with SQL Server 8.0 back end. I have the requirement to import all text files (regardless of filename) from a given folder on the network into a table within SQL Server. This import needs to run continuously, as more text files will be saved in the folder by a separate system and they need to be updated into the SQL Server table. I have a DTS which can import all text files from the...
1
8091
by: srihari | last post by:
Hai, I am trying to install IBM DB2 8.1 on Red Hat linux 8.0. My machine is Intel XEON 64bit. The installation went well except for the creation of tools catalog. When I tried to install the tools catalog as a post installation task I get some java error. $ db2 CREATE TOOLS CATALOG SYSTOOLS USE EXISTING DATABASE TOOLSDB FORCE SQL22209N The DB2 Administration Server encountered an unexpected Java error on host "". I have jdk1.3 already...
6
3722
by: Ken Allen | last post by:
I am relatively new to .Net and C#, but I hav ebeen programing in other languages and done some COM work for a number of years. I am attempting to understand how to map an older program architecture into .Net -- not looking to do it at this time, just to understand how I would achieve it. In the old environment, we had two classes, a client and a server class, that managed a data object. The server object knew how to interface with the...
15
2464
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for objects that the app used to speed up some global level data access and functionality. I have recoded the class libraries in .net and would like to acomplish the same functionality in asp.net.
2
1543
by: tshad | last post by:
I don't know if this can be done. It is a coordination with my client side vs my server side code. I can create a popup fine: Dim myNextButton As ImageButton = CType(DataList1.Controls(DataList1.Controls.Count-1).FindControl("DownQuestion"),ImageButton) myNextButton.Attributes.Add("onclick", "return confirm('Are you sure you want finish this test?');")
1
2545
by: Kevin | last post by:
I'm using a MagTek check / credit card scanner to scan checks and then submit the check data to NovaInfo through their viaWarp Gateway. The NovaInfo people won't even return my emails, so I'm hoping someone here can help me. The scanner outputs the image in a base64 encoded text string. I'm using the following code to POST the data: web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
37
2734
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
2
3578
by: VMI | last post by:
I have a LinkButton_search on my Page1.aspx that opens up a popup page called popup.aspx. I do this with LinkButton.Attributes.Add() on the Page_Load of Page1.aspx. How can I add server-side code to LinkButton_search_Click() so that the code in there runs before opening the popup window? The problem is that I fill a Session variable when I click on the button that will then be used in the Page_Load of my Popup window. So basically this is...
1
29343
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
8428
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
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
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
5650
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();...
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.