![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. ![]() |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
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? |
#5
| |||
| |||
|
|
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. |
#6
| |||
| |||
|
|
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! 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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |