JSFiddle wrap in onLoad?

前端 未结 3 1405
无人及你
无人及你 2020-12-17 01:01

I\'m trying to understand how JSFiddle \'wraps\' code in \'onLoad\' based on this description: [1]: http://doc.jsfiddle.net/basic/introduction.html#javascript. I\'ve seen on

相关标签:
3条回答
  • 2020-12-17 01:18

    They put all your JS inside <script> tags with the onLoad event code wrapped around it.

    For example, if you choose to include jQuery and onLoad then this is what jsfiddle will use:

    <script type="text/javascript">
        //<![CDATA[
            $(window).load(function(){ /* your js here */ });
        //]]>
    </script>
    

    If you don't include a library then they use:

    <script type="text/javascript">
        //<![CDATA[
            window.onload=function(){ /* your js here */ }
        //]]>
    </script>
    

    I presume they also use other library specific load events depending on what you choose to include.

    Using $(document).ready(function(){ }); isn't required when running your code in a fiddle.

    Note: for a good explanation of what CDATA is take a look at this answer - https://stackoverflow.com/a/7092306/2287470

    0 讨论(0)
  • 2020-12-17 01:27

    It makes a new iframe to run your code in.

    For the onload, they just put all your code in

    window.onload = function () {
        // your code here
    };
    

    You can see this if you inspect the <script> tag in the <head> of the bottom-right iframe.


    Nothing wrong with using $(document).ready(... but it's kinda useless when you enable the onLoad option in jsFiddle.

    0 讨论(0)
  • 2020-12-17 01:29

    if you are looking for source code formatting, then you should look for site like below. As to what jsfiddle use they might have their own plugins which does the same.

    http://jsbeautifier.org/

    http://www.javascriptbeautifier.com/

    http://www.jspretty.com/

    0 讨论(0)
提交回复
热议问题