Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an effective way to find which JavaScript includes are not used?

Tags:

javascript

I've inherited a page that is extremely complex and has a ton of <script src=... includes.

I suspect a lot of the includes are not used. Is there a way to figure out which of them are not used?

like image 275
AngryHacker Avatar asked Sep 08 '17 20:09

AngryHacker


People also ask

How can I tell if a website is using JavaScript?

Yes there is a way to look at all of a webpages html, Javascript files, and CSS using Chrome. Open up the page in chrome, right click on the page and select inspect element (or press ctrl+shift+I). From there you can navigate the different elements on the the page and the structure of the HTML file.

Does JavaScript work without?

Yes, JavaScript can be used without html. Node is another option. JavaScript was originally a web scripting language until node js was introduced.

How many ways can you use JavaScript?

Including the JavaScript You can include JavaScript in your HTML in two ways: Writing the code in your HTML. Including it as a link to an external file.

What is the meaning of in JS?

The in operator returns true if the specified property is in the specified object or its prototype chain.


1 Answers

Chrome 59 includes a new Coverage tool, which can be enabled from the 3-dot menu in DevTools.

Chrome DevTools -> Options -> More tools -> Coverage

You should be able to enable the tool, navigate the website and perform scripted actions, and then view which lines were actually called. Files that are not called will appear entirely red (no lines run).

Edit: As mentioned in the comments, this is still not an optimal solution, since it will only detect lines which have actually run. As @adeneo mentioned, it is almost impossible to statically determine which parts of the code will run, simply because of the complexity of JS.

If this is an XY Problem actually intended to decrease the number of initial HTTP requests, it may be a good idea to simply concatenate all required files and minify that (HTTP/1.1), or investigate grouping related assets and serving via HTTP/2.

like image 147
Scott Avatar answered Oct 31 '22 17:10

Scott