Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



javscript/jsfiddle help


2 replies to this topic - - - - -

#1 Guth

    Alba Gu Bráth

  • 788 posts
  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

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

    Resident Fanatic

  • 827 posts
  • Joined: 21-September 07

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

    Alba Gu Bráth

  • 788 posts
  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 18:01

View PostZakO, on 12 December 2012 - 17:59, said:

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.