HighDots Forums  

Function is not defined (but it is)

Javascript JavaScript language (comp.lang.javascript)


Discuss Function is not defined (but it is) in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
K Viltersten
 
Posts: n/a

Default Function is not defined (but it is) - 05-19-2008 , 12:16 PM






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

Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: Function is not defined (but it is) - 05-19-2008 , 12:20 PM






K Viltersten wrote on 19 mei 2008 in comp.lang.javascript:

Quote:
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)


Reply With Quote
  #3  
Old   
Tom de Neef
 
Posts: n/a

Default Re: Function is not defined (but it is) - 05-19-2008 , 12:21 PM



"K Viltersten" <tmp1 (AT) viltersten (DOT) com> schreef in bericht
newsp.ubestuebi74t80 (AT) lp028 (DOT) 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



Reply With Quote
  #4  
Old   
K Viltersten
 
Posts: n/a

Default Re: Function is not defined (but it is) - 05-19-2008 , 01:09 PM



Quote:
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


Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Function is not defined (but it is) - 05-19-2008 , 01:21 PM



K Viltersten wrote:
Quote:
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.

Quote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
// Plone, register_function.js:16


Reply With Quote
  #6  
Old   
K Viltersten
 
Posts: n/a

Default SV: Function is not defined (but it is) - 05-20-2008 , 04:37 PM



Quote:
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.

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!

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.