1

I'm more or less building a new design into some software and to retain the functionality of some of the page features I need to keep some of the javascript files the system uses; however it appears the software uses a global header to include all the .js files and to cut down on http requests I was only wanting to include them when the page actually needed them.

However without actually pining through the code of each page, is there a quicker method you can use to test if the page actually needs to have a certain .js file included or not!?

3
  • Not reliably. JavaScript is too dynamic and is acting on too uncertain of an environment (the DOM can be changed heavily by various means) for that to be determined automatically. Commented Nov 18, 2012 at 14:45
  • @MattWhipple I thought it was a long shot, but thought I'd ask anyway. Commented Nov 18, 2012 at 14:47
  • This is one of those situations where the normal use of the global namespace really hurts also Commented Nov 18, 2012 at 14:48

2 Answers 2

3

There is no reliable way to test that. A better solution would be to pack all javascript files in a single file, so it needs only a single request.

During that packing you may minimize them as well (remove comments and unneeded whitespace). You can cache the packed file, so it doesn't have to be generated on each request. If you pass the proper headers, the browser will cache it as well, saving you bandwidth and speeding up the page.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Yep, have already been looking into some caching mechanisms for many of my static files such as this. :)
Generally this is a better solution than the conditional loading you were looking for, since the cost of the initiation of a request is normally higher than the actual transfer of data. So unless you have some huge javascript, you're better off having the unused code already loaded.
1

TL/DR

No. Due to the dynamic nature of JavaScript it's impossible to tell if a library is used until it's actually run.

Long Answer

If running the application is an option (i.e. it has a defined number of things to test to verify all functionality) then it's possible to write a proxy for the libraries you're considering removing that would log their actual use. It's a bit of an overkill solution and it would probably be easier to just try removing the libraries one by one and testing the pages to see if they work. It's grunt work but the direct method would probably be the fastest.

Related

You probably could speed things up by combining and minifying the includes. If they're all in one place that would probably be easy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.