What JSFiddle Is Doing
onLoad
JSFiddle wraps any code in the JavaScript/jQuery box in $(window).load(function(){...});
. This is because you have the second drop down in Frameworks & Extensions set to onLoad. With this setting, the script will wait for all media and data to load and the page to be rendered.
onDomReady
If you the onDomReady option, JSFiddle uses $(function(){...});
Which just waits for the DOM to load, not necessarily all the data.
What You Can Do
To make you code behave as the JSFiddle code does, you can use either of the above functions.
Another common way to write similar code is $(document).ready(function(){...});
. like the onDomReady code, this only waits for the DOM to load. In my opinion, this is the most commonly used method because it is the most intuitive.