简体   繁体   中英

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??

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.

it works perfectly if you move your javascript to your header

http://jsfiddle.net/D2dxA/2/

<head>
<script>
function message(){
  alert("message"); 
}
</script>
</head>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM