By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

server side global variable failure

P: n/a
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,

Jan 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Bruce" <ba********@yahoo.ca> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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="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,


The following works:

[testGlobal.js follows]

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

[testGlobal.asp follows]

<%@ LANGUAGE="JAVASCRIPT" CODEPAGE="1252" %>
<!--#include file="test~Global.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

P: n/a


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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 20 '06 #3

P: n/a
> 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

P: n/a
"Bruce" <ba********@yahoo.ca> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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="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.
Jan 20 '06 #5

P: n/a
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

P: n/a
"Bruce" <ba********@yahoo.ca> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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 discussion thread is closed

Replies have been disabled for this discussion.