Jump to content



Photo

javscript/jsfiddle help


  • Please log in to reply
2 replies to this topic

#1 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: Nokia Lumia

Posted 12 December 2012 - 17:51

I've been learning javascript and today I thought I'd use jsfiddle to practise on since its quick and easy. but nothing I did was working properly, so I stripped back my code and just did a simple hello world alert on button click. Even that doesnt work!
I'm pretty confident my code is correct but it wont work. Is it jsfiddles fault or mine?

http://jsfiddle.net/JGuth/SmaAM/
That is with the javascript in the javascript box.

If i put the javascript in <script> tags then into the HTML it works then :s As you can see here: http://jsfiddle.net/JGuth/SmaAM/1/

The two are the exact same to my knowledge. So whats going on


#2 ZakO

ZakO

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 21-September 07
  • Location: Finland

Posted 12 December 2012 - 17:59

By default JSFiddle doesn't add functions to the global/window scope, it wraps them inside an anonymous function, e.g:


<script type='[url=""]text/javascript[/url]'>//<![CDATA[
window.addEvent('load', function() {
function myFunction() {
	alert("Hello World!");
}
});//]]>

If you choose "No wrap (head)" instead of "onLoad" under the framework section (in the sidebar) it will work as expected.

http://jsfiddle.net/SmaAM/2/

#3 OP Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: Nokia Lumia

Posted 12 December 2012 - 18:01

By default JSFiddle doesn't add functions to the global/window scope, it wraps them inside an anonymous function, e.g:


<script type='[url=""]text/javascript[/url]'>//<![CDATA[
window.addEvent('load', function() {
function myFunction() {
	alert("Hello World!");
}
});//]]>

If you choose "No wrap (head)" under framework section (sidebar) it will work as expected.

thank you :)
I tried it here: http://jsbin.com/welcome/61091/
and it worked, couldn't understand what was going wrong.