This question already has an answer here:
I have one query, for that I wanted to prepare a JSFiddle. But it's not working for a small click program. Which has just two lines of code.
HTML
<body>
<button onclick="javascript:launchdialog();">Click Me</button>
</body>
JavaScript
function launchdialog() {
alert('test');
}
I didn't find anything wrong in just two lines of code. Please have a look at this JSFiddle - http://jsfiddle.net/g47qqzra/
You can set the option to
No wrap
in head or body.When you use
onload
oronDomReady
, your code is wrapped inside another function that is invoked onload
orready
event. So, your function is not accessible from outside of that function. And you'll get errorMaking the function
no wrap
makes it global, can be accessed from anywhere.Updated fiddle
Jsfiddle Doc
On the left set "
No wrap - in <head>
"Here is the updated JSFiddle link
onLoad:
onLoad
window event. This runs when the entire page has loaded (such as images).onDomReady:
onDomReady
window event. This runs when the DOM has loaded.no wrap - in
<head>
:<head>
sectionno wrap - in
<body>
:<body>
sectionI would like to note that more information can be found in jsFiddle's documentation.
Your working Fiddle
This is because you script is not loaded and the function
launchdialog()
is not found when the page is loaded. If you place the script in head tag, it will be loaded before the page is loaded, hence solve your issue. When you use 'onLoad' option script is loaded only after the document is loaded i.e. in the end. Just select "No wrap - in " on the left set