Function is not defined (but it is)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    Function is not defined (but it is)

    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
  • Evertjan.

    #2
    Re: Function is not defined (but it is)

    K Viltersten wrote on 19 mei 2008 in comp.lang.javas cript:
    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)

    Comment

    • Tom de Neef

      #3
      Re: Function is not defined (but it is)

      "K Viltersten" <tmp1@vilterste n.comschreef in bericht
      news:op.ubestue bi74t80@lp028.p agero.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


      Comment

      • K Viltersten

        #4
        Re: Function is not defined (but it is)

        >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

        Comment

        Working...