Why does jsfiddle throw error that function is not

2019-01-15 20:01发布

问题:

This question already has an answer here:

  • Why isn't my JavaScript working in JSFiddle? 7 answers

The example I was testing is very simple: just one button, and when button is clicked then a function will be called. However, I'm getting:

error ReferenceError: AddNewData is not defined {"error": "Please use POST request"}

Here is the jsfiddle link: http://jsfiddle.net/tridip/62Ls6x9n/158/

<button onClick="javascript:AddNewData();return false;">Add New Data</button>
function AddNewData()
{
    alert("test");
}

Can someone tell me what is wrong there?

回答1:

It's because by default jsfiddle uses onLoad JS definition for your code.

That's it - it's wrapped in an anonymous function so all symbols (variables, functions, etc) are scoped within.

Change it to No wrap - in <head>

http://jsfiddle.net/62Ls6x9n/160/