Javascript function not called in "fiddle"

I made this short script: http://jsfiddle.net/NZnN2/ . Why does this not appear when I press the button?

+7
source share
2 answers

Since your JavaScript code is in the onload handler, which means that display_message not global and therefore HTML is not available.

Since you selected onload , your JavaScript is injected into the page as follows:

 window.addEvent('load', function() { function display_message(){ alert("woohoo!"); } }); 

As you can see, display_message is only available inside this anonymous function. To make a working example, change onload to no wrap (head) or no wrap (body) (on the left side of the page).

Working example: http://jsfiddle.net/NZnN2/8/

+11
source

instead

 function display_message() { alert('woohoo!'); } 

do

 display_message = function () { alert('woohoo!'); } 
+3
source

All Articles