3

I have an application with multiple pages which loads dozens of external scripts.

Some pages include other pages, which means several scripts are loaded multiple times.

Is there any way I cold check which scripts are being loaded and then stop them from being loaded a second time? And improve performance.

Sorry I have no code examples, I am just beginning to tackle this issue.

Would anyone have any suggestions?

Daft
  • 10,277
  • 15
  • 63
  • 105
  • 2
    .js files are cached once they are loaded.So i think it will not affect the performance. – Zword Nov 20 '13 at 11:06
  • Sorry, but your question is indeed too broad at the moment. You didn't mention how your application creates the pages, and which parts of it you're able to change. – raina77ow Nov 20 '13 at 11:07

3 Answers3

3

Your browser will do this for you - if the scripts are being loaded from the same URL - as the browser will cache the JavaScript files and load them form the cache rather than requesting them over HTTP.

If different pages load the same script (for example jQuery) but from different URLs, both will be requested as the cache is per-address.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • That's pretty much what I was thinking Steve, but I still have to write this function... – Daft Nov 20 '13 at 15:42
3

Yes, your scripts can create objects and you can prevent reload if the object exists already

See Can i use javascript to prevent Loading the same script over and over? for more information

Another way round the problem is to use a module loader such as require.js to manage loading your Javascript files. You will have to convert your Javascript files into "modules" which are compatible with require, but the module format is widely used. As this is a standard and not a home-brewed approach it is possibly a better idea than my previous suggestion.

Community
  • 1
  • 1
vogomatix
  • 4,856
  • 2
  • 23
  • 46
  • vogomatix, Ive looked through the link u added but am still having trouble. Could u perhaps explain ur answer a little more? – Daft Nov 20 '13 at 15:19
  • I hate to say this but after thinking on this I changed my mind a little as to the best solution, please see my amended answer. – vogomatix Nov 21 '13 at 07:43
1

I'd place a console.log("scriptname - loaded") in each distinct file. Then, using console you can see which scripts are loaded multiple times.

If you include a page, it really shouldn't have all the generic scripts and headers in anyway...

Pat Dobson
  • 3,249
  • 2
  • 19
  • 32