Uncaught ReferenceError For A Function Defined In An Onload Function
Solution 1:
Your JSFiddle preferences are set to "onload", so the content of the JavaScript pane is wrapped in a function.
This scopes cc
(dd
does not exist, creating an additional problem for the jsfiddle example) to that function and stops it being a global.
Since you are trying to call it from an intrinsic event attribute, you are in a different scope and cannot access it.
As a quick fix, you can change the preference to 'nowrap', however that won't make the code conform to best practises.
It is recommended to avoid intrinsic event attributes in favour of binding event handlers with Javascript. YUI3 provides an event module and jQuery provides the on
method to help with this (other libraries are available). See also: Unobtrusive JavaScript.
For example, see this js fiddle.
Solution 2:
It does, you just have two errors in your fiddle:
- You called
dd
instead ofcc
- You wrapped your whole javascript in a onload-function (see dropdown on the left), and the variables and functions are local to that. Move it to the global scope, or assign the
cc
function to the global object:window.cc = function(){…};
Solution 3:
window.cc = function(id) { ...
the variables and functions seem to be in their on scope...
Post a Comment for "Uncaught ReferenceError For A Function Defined In An Onload Function"