Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

Defer parsing of JavaScript - possible ?

vanillervaniller
edited December 2011 in Vanilla 2.0 - 2.8

Google page Speed tells, that on my vanilla home page about 120kB of JavaScript code should be deferred (I.e. /js/library/jquery.js, js/global.js etc.)
Is it posiible, and if so, how can in make it ?

Tagged:

Best Answers

  • Options
    422422 Developer MVP
    Answer ✓

    There was an error rendering this rich post.

  • Options
    SS ✭✭
    edited December 2011 Answer ✓

    Plugin <Page Speed> can do this. But it cannot handle with dependent inline scripts (*), they will be broken.

    * dependent inline scripts, I meant such as jQuery().ready(){...};

  • Options
    ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    I recently added support for <script defer="defer" ... >. This is supposed to do the same thing, but it doesn't work in as many browsers. It worked just fine in Firefox and chrome for me. IE supposedly invented the attribute so I'll assume it works there too.

    Which scripts can be deferred is is another question. I don't think jQuery is one of them since pretty much every other script makes use of jQuery.ready(...).

Answers

  • Options
    TimTim Operations Vanilla Staff

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Options
    422422 Developer MVP
    Answer ✓

    There was an error rendering this rich post.

  • Options
    422422 Developer MVP
    edited December 2011

    The seemingly adopted and preferred method is using async.

    <script>
    var node = document.createElement('script');
    node.type = 'text/javascript';
    node.async = true;
    node.src = 'example.js';
    // Now insert the node into the DOM, perhaps using insertBefore()
    </script>
    

    There was an error rendering this rich post.

  • Options
    SS ✭✭
    edited December 2011 Answer ✓

    Plugin <Page Speed> can do this. But it cannot handle with dependent inline scripts (*), they will be broken.

    * dependent inline scripts, I meant such as jQuery().ready(){...};

  • Options
    ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    I recently added support for <script defer="defer" ... >. This is supposed to do the same thing, but it doesn't work in as many browsers. It worked just fine in Firefox and chrome for me. IE supposedly invented the attribute so I'll assume it works there too.

    Which scripts can be deferred is is another question. I don't think jQuery is one of them since pretty much every other script makes use of jQuery.ready(...).

Sign In or Register to comment.