/    Sign up×
Community /Pin to ProfileBookmark

with runat=server, function execute fine, but variables are undefined – why?

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() {}
[/CODE]

This declaration, however, produces a runtime error ‘Object Expected’ on the line creating the new product.

[CODE]
var Product = function() {}
Product.prototype.addToCart() {}
[/CODE]

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?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarOct 23.2007 — I know that in VB you must declare variables on a seperate line to which you first use them. I also know that ASP and VB use pretty much the same language.

basicly I would suggest trying it after spliting the code over another line like so:
var Product
Product = function() {}
Product.prototype.addToCart() {}
but that's only a guess based on what I was forced to learn at college about VB.
Copy linkTweet thisAlerts:
@iporterauthorOct 23.2007 — ASP and VB use pretty much the same language[/QUOTE]
For your information, this is not actually quite the case. ASP requires the use of a scripting language, which could be VB script, but can also be javascript - i guess whatever the browser supports. VB & Javascript are commonly supported.

Any further thoughts?

Cheers,

Iain
×

Success!

Help @iporter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...