使用 JavaScript 处理重复的 HTML 数据好不好?
假设您有一个网站所有页面都相同的菜单。您应该使用客户端脚本还是服务器端脚本?
Let's say you have a menu that's the same for all pages of a website. Should you use client-side or server-side scripting for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在服务器端生成它并缓存它。为什么要让每个浏览器都做一些工作?
您可以为每个页面生成/输出相同的响应,而不是让 javascript 进行额外的调用或导致菜单仅在脚本运行完毕后才出现。
I would generate it on the server side and cache it. Why make every browser do some work?
You can generate/output it with the same response for each page instead of having javascript make extra calls or cause the menu to only appear after the script finished running.
如果是重复内容,我会通过
加载客户端脚本,以便重复内容可以单独缓存,以减少用户必须下载的数量。但需要注意的是,如果您的网站必须在没有启用 JavaScript 的情况下工作,那么显然您必须在服务器端执行此操作。
通过菜单,您还可以推迟脚本,以便它可以与用户访问网站要查看的主要内容并行下载。
If it's repetitive content, I would do client-side script loaded via
<script src=...>
so that the repetitive content is separately cacheable to reduce the amount your users have to download. One caveat though, if your site has to work without javascript enabled, you obviously have to do it server-side.With menus, you can also make the script deferred, so that it can download in parallel to the main content that your users came to the site to see.