开发者

Why does this simple JSFiddle not work? [duplicate]

This question already has answers here: Why isn't my JavaScript working in JSFiddle? (7 answers) Closed 4 months ago.

There is some code I wanted t开发者_JAVA技巧o put into JSFiddle. It didn’t work. Narrowing it down I can’t even get this simplest of code to work:

JSFiddle

function displaymessage() {
  alert("Hello World!");
}
<form>
  <input type="button" value="Click me!" onclick="displaymessage()" />
</form>

<p>By pressing the button above, a function will be called. The function will alert a message.</p>

The alert box doesn’t show up in the JSFiddle.


Select No wrap - bottom of <head> in the “Load type” dropdown in the JavaScript settings.

Why does this simple JSFiddle not work? [duplicate]


You need to take your function out of the onLoad/onReady otherwise it is placed inside of another scope and your button cannot access the function. In your case you need to use No wrap (head)

The code generated looks like this:

Ext.onReady(function() {
    function displaymessage()
    {
        alert("Hello World!");
    }
});


Change the code to run "no wrap (head)" instead of "onDomReady". Your function isn't visible to your markup as is.


Maybe some of you need to move your function outside of document.ready if you use html onclick="func()"

$(document).ready(function() {
  ...
}

function func(){
  ...
}


If you still have problems, check whether you have

  1. AdBlockers installed as a browser extension? (i.e. AdBlocker Plus)

Why does this simple JSFiddle not work? [duplicate]

  1. Turn off AdBlockers in JSFiddle and run again.

I hope this helps some one.

Thanks.


Sorry but... is much simpler than what you propose...

If you want to call a js function with "onclick" attribute, put your javascrit code directly in head, you dont need domready or similars.

Second, you need to include "javascript:" before the function name.

See: http://jsfiddle.net/XNJxT/1838/

<input type="button" value="Click me!" onclick="javascript:displaymessage()" />


My problem to this was when I made my script tag I typed it out :

script type="javascript/text"

instead of :

script type="text/javascript"

and now it works.


http://jsfiddle.net/praveen_prasad/XNJxT/14/

Js fiddle so something like this to whatever you write

window.addEvent('load',function(){

//Your code


});

Why does this simple JSFiddle not work? [duplicate]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜