Search code examples
javascriptjsfiddlejsbin

javascript alert function strange behaviour


So I am learning JavaScript.... I have gone way past this basic stuff in my learning but can't figure out why this wont work:

   function message(){
      alert("message"); 
    }

<button onclick="message()">Click me</button>

On Js Bin all is ok http://jsbin.com/uwutut/1/edit

On Js fiddle the alert is not triggered!! http://jsfiddle.net/vincentieo/D2dxA/

It is the exact same code...copy and paste so no reason why js fiddle is being fussy??


Solution

  • The jsfiddle infrastructure puts your code in an event handler for the "load" event if you don't tell it not to. That means that your function is a local function to that handler, so it's not visible globally.

    On the left side of the jsfiddle UI, look for a pull-down about where the code goes, and choose "no-wrap (body)". See if that helps.