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

Function is not defined (but it is)

P: n/a
I declared a function in a file called somefile.js as follows:

function foo() {
alert ("yippi!"); }

In HTML-file i'm importing it by adding:

onClick='foo()'

The somefile.js is imported correctly (according to my breakpoints in
FireBug SOME code in the file is reached). Despite of that, as i cause the
onClick-event to fire, i get the error message: "Function is not defined".
How can i debug it? As far i can see, it should work. :)

--
Regards
Konrad Viltersten
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
K Viltersten wrote on 19 mei 2008 in comp.lang.javascript:
I declared a function in a file called somefile.js as follows:

function foo() {
alert ("yippi!"); }

In HTML-file i'm importing it by adding:

onClick='foo()'

The somefile.js is imported correctly (according to my breakpoints in
FireBug SOME code in the file is reached). Despite of that, as i cause
the onClick-event to fire, i get the error message: "Function is not
defined". How can i debug it? As far i can see, it should work. :)
You made an error somewhere in your code, but how can we say,
if you only describe your code?

Start temporarily importing the js file in your page code for testing.
Read the error descriptions [all of them] and the error lines.
Place breakpoints.
Try diferent browsers.

Etc, all the usual stuff.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #2

P: n/a
"K Viltersten" <tm**@viltersten.comschreef in bericht
news:op***************@lp028.pagero.local...
I declared a function in a file called somefile.js as follows:

function foo() {
alert ("yippi!"); }

In HTML-file i'm importing it by adding:

onClick='foo()'

The somefile.js is imported correctly (according to my breakpoints in
FireBug SOME code in the file is reached). Despite of that, as i cause the
onClick-event to fire, i get the error message: "Function is not defined".
How can i debug it? As far i can see, it should work. :)

Are you sure that the declaration of foo() is in the global scope and not
inside another function?
Tom
Jun 27 '08 #3

P: n/a
>I declared a function in a file called somefile.js as follows:
>>
function foo() {
alert ("yippi!"); }

In HTML-file i'm importing it by adding:

onClick='foo()'

The somefile.js is imported correctly (according to my breakpoints in
FireBug SOME code in the file is reached). Despite of that, as i cause
the
onClick-event to fire, i get the error message: "Function is not
defined".
How can i debug it? As far i can see, it should work. :)

Are you sure that the declaration of foo() is in the global scope and not
inside another function?
Well, i checked for that parentesis-wise a while ago, so yes, i was sure..
Then you got me thinking and i tried to move the function around, just for
the sake of testing. And guess what - it turns out that the function
definition needs to be at the top of the file, before any other code
(except for comments, blanks and other functions, i guess)! That's
extremely surprising!

I'd expect the function definition to be in global scope if it's declared
OUTSIDE of any {}-pair but apparently, it's not sufficient. Problems
solved. Thanks! You were right about being out of scope, even if it didn't
appear that way to me.

--
Regards
Konrad Viltersten
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.