0

I want to create a function that will execute when my page loads. Normally I would put this function in the body's onload attribute. I'm a little unsure how to do that in JSFiddle. I have this in my script ...

window.onload=function(){ alert('hi'); }

but nothing happens when I load my page -- https://jsfiddle.net/xx77tzn3/3/ . How do I execute a function in JSFiddle when the body loads?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • jsFiddle has a configuration option to put all the code in the JavaScript pane in either a "load" handler, or in the body. Click on the little button in the upper right corner of the panel. – Pointy Aug 02 '17 at 20:27
  • The default is to put your code in a "load" handler, so when the "load" event fires, and you set up another "onload" function, it'll never run because there won't be another "load" event. – Pointy Aug 02 '17 at 20:29
  • Possible duplicate of [jsFiddle: no connection between html and js? Can't call simple function from button?](https://stackoverflow.com/questions/14499783/jsfiddle-no-connection-between-html-and-js-cant-call-simple-function-from-but) – Teemu Aug 02 '17 at 20:33
  • @Pointy, what if I don't want to put all the JS pane code in the onload, but rather, define a function in there and then have that function execute in the onload? – Dave Aug 02 '17 at 20:37
  • @Teemu, I think the JSFiddle settings have changed since 2015, when taht question was posted. I don't see a "No wrap (Head)" setting anywhere. – Dave Aug 02 '17 at 21:28
  • 1
    Yep, they have a new layout, the settings are still the same. There it is, in the "Load type" drop down, the third option ... Take a look at the top right corrner of JS pane, There you can see a button with text "JAVASCRIPT" and a gear icon, click that button and you'll find what you need. – Teemu Aug 02 '17 at 21:28
  • Thanks for pointing out the new location of the setting. Taht solved it. – Dave Aug 03 '17 at 15:23

0 Answers0