0

I know this is probably something really dumb, but it really pisses me off for a good 15 minutes. What am I missing here?

<input type="button" value="Go" onclick="showAlert()">

function showAlert() {
    alert('???');
}

Fiddle

iuliu.net
  • 6,666
  • 6
  • 46
  • 69
  • https://jsfiddle.net/madhawa_R/evmzwwwe/3/ – Madhawa Priyashantha Jun 05 '16 at 16:45
  • it largely depends where showAlert() is defined, there are no syntax errors in your code, which is a good starting point anyway. – briosheje Jun 05 '16 at 16:46
  • 1
    The most unfortunate "feature" of jsFiddle. I'd love it if when any "wrap" option is chosen, they have an automatic, uneditable line at the top and bottom of the edit area showing the wrapper. –  Jun 05 '16 at 16:50

1 Answers1

3

Because in that fiddle you've chosen to define the function "onLoad".

That's same as defining it in window.onload=function(){...}, which makes it unaccessible outside the onload scope (i.e. you can call it only from that onload), as happens with any nested functions.

You should define that function in the <head>, choose the third option.

nicael
  • 18,550
  • 13
  • 57
  • 90
  • `` would work too. Anything that doesn't wrap it. –  Jun 05 '16 at 16:54
  • @squint It will, but that's already JSFiddle specific. Yes, it would place the script after other body contents, but user may not know this and run into problems once (or if) they "convert" their fiddle to the full html page. Defining in head ensures that it's easy to port that to the full html page, because you can place the script as after the title or before the title (just an example), the order doesn't really matter. – nicael Jun 05 '16 at 16:57
  • It shouldn't matter even when converting to an actual app, unless they add other scripts above that one that try to invoke it, but then that would also be an issue in the ``. I guess if a visitor manages to somehow fire off an event while the document is loading, it would fail, but that seems pretty unlikely. –  Jun 05 '16 at 17:03