server side global variable failure

B

Bruce

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="JAVASCRIPT" CODEPAGE="1252"%>
<script language="JavaScript" 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,
 
M

Martin Honnen

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.methodName = 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.
 
B

Bruce

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.
 
M

McKirahan

Bruce said:
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="JAVASCRIPT" CODEPAGE="1252" %>
<!--#include file="testGlobal.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.
 
B

Bruce

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?
 
M

McKirahan

Bruce said:
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.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top