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!

<script runat=server></script> produces vars being undefined - why?

Status
Not open for further replies.

isporter

Programmer
Oct 18, 2007
11
GB
I plan to create an object in a javascript file, and then use that object and it's methods on both the server and the client side. Assume an ASP file with the code 'var product = new Product();'

The following declaration in the javascript file works fine:
Code:
function Product () {}
function Product.prototype.addToCart() {}

This declaration, however, produces a runtime error 'Object Expected' on the line creating the new product.
Code:
var Product = function() {}
Product.prototype.addToCart() {}

Simplifying the issue, I found that if I simply declared a string with 'var text = "hello world!";', and then called 'Response.Write(String(text));' in my asp page, it returns 'undefined'. However, if I remove the variable declaration from the javascript file, it returns a runtime error: 'text' is undefined.

Can anyone explain this behaviour and provide a solution?
 
Client and server-side scripting don't interact like that. What's on the server is on one computer, and what happens on the client is a completely different computer disconnected from the server.

Lee
 
Lee, thanks for your response. I'm concerned I didn't make myself clear, so let me expand.

Assume a test.js file with
Code:
function Product () {}
in it.

You can include this script in an ASP page as follows:
Code:
<%@ language="javascript"%>
<script language="Javascript" src="test.js" runat="server"></script>

Then you can use
Code:
<% var myProduct = new Product() %>
in the ASP file to create a server-side instance of the Product object.

However, if in my .js file I use the notation
Code:
var Product = new function() {}
, it doesn't work. Why is this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top