Uncaught ReferenceError for a function defined in onload function
HTML:
<div id="myID" onclick="cc(this.id)">Click Here</div>โ JavaScript:
var timer; var firing = false; var begen = function(id) { alert('tek'); }; var popupAc = function(id) { alert('รงift'); }; function cc(id) { if (firing) { popupAc(id); clearTimeout(timer); firing = false; return; } firing = true; timer = setTimeout(function() { //begen(id); clearTimeout(timer); firing = false; }, 250); } Error:
Uncaught ReferenceError: cc is not defined Example: http://jsfiddle.net/LXSZj/3/
Your JSFiddle settings are set to "onload", so the contents of the JavaScript panel are wrapped in a function.
These areas are cc ( dd does not exist, creating an additional problem for the jsfiddle example) for this function and stops it as global.
Since you are trying to call it from an attribute of an inline event, you are in a different area and cannot access it.
As a quick fix, you can change the "nowrap" preference, however this will not make the code compatible with best practices.
It is recommended that you avoid the attributes of inline events in favor of binding event handlers using Javascript . YUI3 provides an event module , and jQuery provides an on method to help with this (other libraries are available). See also: Unobtrusive JavaScript .
For example, see this js script .
You just have two errors in your violin:
- You called
ddinstead ofcc - You wrapped all javascript in an onload function (see the drop-down list on the left), and the variables and functions are local for this. Move it to the global scope or assign the
ccfunction to the global object:window.cc = function(){โฆ};