查找所有未使用的 javascript 文件
我继承了一个遗留项目,里面有大量的 javascript 文件...... 有没有办法找到哪些文件在页面内使用?
提前致谢。
I've inherited an legacy project with tons of javascript files all over the place...
Is there a way to find which of those files are used inside pages?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用像YSlow这样的调试工具!
http://developer.yahoo.com/yslow/
此类工具通常会指出冗余文件和代码。
Use a debugging tool like YSlow!
http://developer.yahoo.com/yslow/
Such tools will usually point out redundant files and code.
您可以检查过去(例如一个月)的网站日志,并找到浏览器请求的所有
*.js
文件。该日志可能包含引用页面,这使事情变得更容易。我经常对
.asp
文件做的事情:找到一个好的文本编辑器,它允许您在文件/文件夹中查找文本。 Visual Studio .NET 做得非常出色,但我也尝试过使用 Notepad++ 并取得了成功。查找包含.js
的所有文件。如果您的文本编辑器提供正则表达式搜索支持(上述产品就是如此),这会让事情变得更好。我在 VS 中使用的正则表达式是\.asp>
(点 asp 后跟字边界)。搜索结果通常显示在一个窗口中,您可以从中复制所有内容并通过更多正则表达式操作进行一些手动处理或将数据复制到 Excel 文件。如果您的网站具有某种结构,Macromedia Dreamweaver 的工作效果还可以接受。 Dreamweaver 中有一个“查找损坏的链接”命令,可以生成名为“孤立文件”的辅助报告。孤立文件报告可以告诉您哪些js文件没有被任何页面引用。然后您可以运行Dreamweaver的“查找”命令(在整个本地网站中查找/在文件夹中查找)来一一仔细检查每个文件。我也尝试过。需要注意的一件事是 Dreamweaver 可能无法检测循环。例如,如果
bar.asp
使用文件foo.js
,但bar.asp
本身没有被任何其他页面引用,Dreamweaver 将标记bar.asp
是孤立的,但foo.js
不是。最新版本的 Dreamweaver 可能会做得更好。You can inspect the website logs of past, say, one month, and locate all
*.js
files requested by browsers. The log might contain referrer page which makes things easier.Something that I do very often for
.asp
files: find a good text editor that allows you to find text inside files/folders. Visual Studio .NET does an excellent job but I've tried and had success with Notepad++ too. Find all files that contain.js
. If your text editor provides regular expressions support for searching (the aforementioned products do) this makes things even better. The regexp I use in VS is\.asp>
(dot asp followed by a word boundary). The search results are often displayed in a window from where you can copy every thing and do some manual processing via more regex operations or copy the data to an excel file.Macromedia Dreamweaver does an acceptable job if your website has some structure in it. There is a "Find broken links" command in Dreamweaver that generates a side report called "Orphaned files". The orphaned files report can tell you which js files are not referenced by any page. Then you can run the Dreamweaver's "Find" command (find in entire local website/find in folder) to double check each file one by one. I've tried that too. One thing to note is that Dreamweaver might not be able to detect cycles. E.g. if a file
foo.js
is used bybar.asp
butbar.asp
itself is not referenced by any other page, Dreamweaver will flagbar.asp
as orphan but notfoo.js
. The recent version of Dreamweaver might do a better job.您可以使用通用脚本语言创建一个脚本,该脚本运行项目中的每个 html 文件,检查它们的脚本标签。或者您也可以手动执行此操作,这可能会也可能不会节省您的时间,具体取决于项目的大小。
You could create a script in a general purpose scripting language that runs through every html file in your project, checking their script tags. Or you could just do it manually, which may or may not save you some time depending on the size of your project.