Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

functions in a <script runat=server> work, but not variables - why?

Status
Not open for further replies.

isporter

Programmer
Oct 18, 2007
11
GB
Hi, throw the following example into a text file, save with an ASP extension, and execute it in IIS6/7. Can anyone explain why Test 1 works fine, and Test 2 returns undefined? Is it something to do with variable scope?

Thanks,
Iain


<%@ language="javascript"%>
<script type="text/javascript" language="JScript" runat="server">
function myFunction() {return 'Hello Functional World'};

var myVariable = 'Hello Variable World';
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "<html xmlns=" lang="en">
<head>
<title>Test</title>
</head>
<body>
<% Response.Write('Test 1: ' + myFunction());%>
<br />
<% Response.Write('Test 2: ' + myVariable);%>
</body>
</html>
 
The first test is a function and you're returning the value from the function. If the second is a variable, then I believe you need to return the .value of the variable. Try this:
Code:
<%@ language="javascript"%>
<script type="text/javascript" language="JScript" runat="server">
function myFunction() {return 'Hello Functional World'};

var myVariable = 'Hello Variable World';
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] lang="en">
<head>
<title>Test</title>
</head>
<body>
<% Response.Write('Test 1: ' + myFunction());%>
<br />
<% Response.Write('Test 2: ' + myVariable[COLOR=red].value[/color]);%>
</body>
</html>
At least, I think that's about right... You may have to play with it a little bit...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Hi Chopstik, I'm not sure that's valid - why would a string suddenly require use of a value property to access it's value?

I tested your suggestion to be sure, but no luck!

Thanks anyway. Any other thoughts?
 
Why are you mixing ASP and runat="server" scripting in the same language on the same page? You'll most likely find that scripting inside ASP tags is processed at different times than scripting in runat="server" tags, and different times depending on the language specified.


Lee
 
I'm trying to do this because I want to declare classes in a .js file that I can then include on both the Server and Client sides. Thus, I can write HTML on the server side (readable by search engines), and update that HTML client-side, using the same class definition.

Is there a way I can do this?

Would it not be correct to suggest that the linked reference is not entirely correct, given that the functions do work, even if the variables do not?

Thanks,
Iain
 
ESquared said:
Their recommendation is "don't mix <%%> and <script runat=server>. Use one or the other."

The Tek-Tips thread and article it refers to show examples of problems that can crop up mixing and matching. You created another example here.

Can you really use the same JS for both server-side and client-side scripting? Have you actually done this?

Lee
 
You can, yes - so long as you don't call server specific object (i.e. Response) on the client-side, or client-specific objects (i.e. document) on the server, there's no reason you can't use the same code, because it's the same language.

So you might define a general object that you include in both, and then extend that object on the client-side with client-specific methods, and on the server with server-specific methods.

But obviously, it would be a lot more useful to be able to include the same file in both, otherwise you'd have to maintain two files each containing the same code for the base objects.

This seems so intuitive, it seems crazy it might not be possible. It seems that functions work fine, it's just global variables. I want global variables so I can define classes as follows:

var myClass = Class.create({
initialize: function() {
...constructor code;
}
});

Any further thoughts?
Iain
 
A more informative example:

4:undefined2: undefined3:local1global

Is produced by the following code:
-------------------------------------------------------
<%@ language="javascript"%>
<script language="javascript" runat="server">
var global = 'global';
Response.Write('1' + global);

function function1() {
Response.Write('2: ' + global);
}
function function2() {
var local = 'local';
Response.Write('3:' + local);
}
</script>
<%
Response.Write('4:' + global);
function1();
function2();
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top