Preload script file

后端 未结 4 1867
深忆病人
深忆病人 2020-12-22 04:25

There are numerous plug-ins to preload images, but is there a way to preload javascript? My application uses a big js file and it take about 5 seconds or so to load before t

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-22 04:49

    Load the script in an iframe and it should have it cached. It may also work if you try to add it as the source for an img tag. Once the rest of your components are done loading, then you can add the script tag with your big script so it's loaded onto the page. I am not sure that will fix your problems though. Are you sure you want to "preload" or just wait until your page is done rendering and then load your big JS script?

    If you want the latter, then you should wait until either the load or document ready event has fired to load the script.

    Tip: You can also put the script on a separate (sub)domain. That allows some browsers to load more resources in parallel so it doesn't block the rest of the site from loading, although many new browsers do that by default.

提交回复
热议问题