Lazy loading JavaScript and Inline JavaScript

后端 未结 7 1488
悲哀的现实
悲哀的现实 2020-12-29 07:31

I noticed in the of my site (for work), there are a lot of and <

7条回答
  •  长情又很酷
    2020-12-29 07:46

    Instead of inline scripts, create inline functions. Then at the end of your javascript file, call the function if it exists.

    function inline_script() {.. code ..}
    

    Inside your concatenated jquery+etc async javascript file:

    if (typeof(inline_script) == 'function') inline_script()
    

    You'll have to do some shuffling to make sure you only have one inline_script per page, or use some kind of caching to put them together if you call it more than once.

    It's still a fun problem in the current age even though this question is old, async scripts = pretty awesome

提交回复
热议问题