How to determine if a javascript was already loaded by other html file

后端 未结 3 2018
小蘑菇
小蘑菇 2021-01-22 23:43

How to determine if a javascript was already loaded by other html file? I want to reduce the redundant loading of the javascript files to decrease the loading time of my webpage

3条回答
  •  花落未央
    2021-01-23 00:30

    As Rory says the second request will probably be cached, and noting that this is a bit of a design failure if it can happen, you can understand that the cached file is still going to execute with negative effect.

    This is horrible, but you could wrap your JS script like this:

    if (!document.foo)
    {
      //your script here
    
      document.foo = true;
    }
    

提交回复
热议问题