Lazy loading JavaScript and Inline JavaScript

后端 未结 7 1467
悲哀的现实
悲哀的现实 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:34

    First of all I would recommend you to analyse the loading of the scripts on the client side very carefully and not only for the first JavaScript loading, but on the loading of the same JavaScript files at the second time or its loading on another page. If the ETag are correctly set on the scripts by the web server or if you use other caching options of HTTP (see the caching tutorial for details) for the JavaScripts files than no loading of the files itself will take place and only the cache revalidation will be done. So it can be that the problem which you describes is not so important like it looks like.

    If you do decide to load some scripts dynamically you can use jQuery.getScript and place all dependent code inside of the success callback. If you need to load one or two JavaScript files the way will work very good, but if you need to load a list of JavaScript files with more complex dependency the implementation could be not so easy. In the case you can use document.writeln inside of . The usage of the method inside of has almost no disadvantages (see here for details).

提交回复
热议问题