Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to identify orphaned classes in a web app built with HTML, JavaScript, and CSS?

I have been trying to delve into web development. Part of this process has been incrementally tweaking things on my website, trying to learn something new with each incremental change.

The problem I'm running into with this is that as the website evolves, I end up doing away with and adding new classes to my HTML elements. Obviously, when I create these selectors in my HTML, I target them in my JavaScript or my CSS, but I often end up going back and doing away with elements. This leaves orphaned references to these selectors in my JavaScript and my CSS, as well as unnecessary/unreferenced classes and ids in the HTML.

My question is is there some utility that I can use to identify these orphaned selectors both in my CSS, HTML, and JavaScript* so that I can safely clean up these files? The solutions suggested so far are great for finding unnecessary selectors in my CSS, but don't seem to help with identifying unnecessary classes or IDs in my HTML, which is also something I'd like to do. Is there something that works both ways as described?

*It sounds like for JavaScript/JQuery there are methods, but for my purposes, my JavaScript is clean enough that I can clean it without help, so its fine if the focus is on CSS and HTML.

like image 749
finiteloop Avatar asked Jan 31 '13 23:01

finiteloop


People also ask

What can CSS do that HTML Cannot?

So, what is the difference between HTML and CSS? Quite simply, HTML (Hypertext Markup Language) is used to create the actual content of the page, such as written text, and CSS (Cascade Styling Sheets) is responsible for the design or style of the website, including the layout, visual effects and background color.

What is HTML CSS and JavaScript called?

If you are learning web development, you will come across terms like HTML, CSS, and JavaScript. These are often called the building blocks of the Web. These three tools dominate web development.

Why is web or browser programming broken into three parts?

A web page is traditionally made up of three separate parts with separate responsibilities: HTML code defines the structure and meaning of the content on a page, CSS code defines its appearance, and JavaScript code defines its behavior.


1 Answers

For CSS there is a Firefox extension that finds unused CSS selectors on a page. It has an option to spider the whole site. Version 3.01 should work with newer versions of Firefox.

https://addons.mozilla.org/en-US/firefox/addon/dust-me-selectors/

And here's another option.

https://addons.mozilla.org/en-US/firefox/addon/css-usage/

for Javascript you are pretty much on your own. If you are using jquery you can override the $() function calling jQuery() inside that function and checking / loging the value returned from jQuery to ensure all selectors are returning some results. But its not a deterministic way of doing things.

like image 88
basarat Avatar answered Nov 15 '22 00:11

basarat