Code in JsFiddle doesn't work
I have one request for which I wanted to prepare a JSFiddle. But it doesn't work for a small click program. Which has only 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 with the two lines of code. Please take a look at this JSFiddle - http://jsfiddle.net/g47qqzra/
source to share
You can set the option No wrap
in the head or body.
When you use onload
or onDomReady
, your code is wrapped inside another function that is called in the event load
or ready
. So your function is not available from outside of that function. And you get the error
ReferenceError: functionName is undefined
Creating a function No wrap
makes it globally accessible from anywhere.
source to share
OnLoad
- This means that it completes the code so that it runs on a window event
onLoad
. This is done when the entire page has loaded (eg images).
onDomReady
- This means that you have to wrap the code so that it runs on a window event
onDomReady
. This is done when the DOM is loaded.
no wrap - in<head>
:
- This will put your JavaScript code in the section
<head>
no wrap - in<body>
:
- This will put your JavaScript code in the section
<body>
I would like to point out that more information can be found in the jsFiddle of the documentation .
Your Work Fiddle
source to share
This is because you script was not loaded and the function was launchdialog()
not found on page load. If you put a script tag in the header, it will be loaded before the page loads, so solve the problem. When you use the "onLoad" parameter, the script is loaded only after the document has loaded, that is, at the end. Just select "No Wrap" in the left set
source to share