Function is not defined (but it is)

K

K Viltersten

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. :)
 
E

Evertjan.

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.
 
T

Tom de Neef

"K Viltersten" <[email protected]> schreef in bericht
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
 
K

K Viltersten

I declared a function in a file called somefile.js as follows:
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.
 
T

Thomas 'PointedEars' Lahn

K said:
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!

It is almost certainly wrong. Variable instantiation comes before execution.
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.

If you posted the URL of the source code not working, in order not to dump
all the source code here, one could analyse what the real problem was.


PointedEars
 
K

K Viltersten

I declared a function in a file called somefile.js as follows:
It is almost certainly wrong. Variable instantiation comes before
execution.


If you posted the URL of the source code not working, in order not to dump
all the source code here, one could analyse what the real problem was.


No need for a poor soul to search through that. It doesn't mean
i not appreciating the offer. I'm simply to stubborn to do it the
easy way. I simply rewrote the whole thing and it worked so i
believe the error could be at some parentheses or something
like that. Relying on your expertise, it MUST have been that.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top