动态填充主页内容?
是否有任何理由不让网页动态检索其主要内容?
例如,我有一个具有页眉和页脚的页面,并且该页面的中间是一个空的 div。当您单击标头中的按钮之一时,会在幕后完成 http GET,并且空 div 的 .innerHTML() 会被结果替换。
我想不出为什么这可能是一个坏主意,但我似乎找不到任何页面可以做到这一点?请指教!
Is there any reason to NOT have a webpage retrieve it's main content on the fly?
For example, I have a page that has a header and a footer, and in the middle of this page is an empty div. When you click on one of the buttons in the header, an http GET is done behind the scenes and the .innerHTML() of the empty div is replaced with the result.
I can't think of any reason why this might be a bad idea, but I can't seem to find any pages out there that do it? Please advise!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这并非闻所未闻,但存在问题。
显而易见的是,一些用户出于安全原因关闭了 javascript,他们将根本无法使用您的网站。
它还会对使用屏幕阅读器等辅助技术的残障用户产生负面影响。
它会使浏览器更难有效地缓存您的静态内容,从而降低浏览体验。
它会使搜索引擎更难将您的内容编入索引。
它可能导致后退和前进按钮停止工作,除非采取特殊步骤使其工作。
它可能导致后退和前进
调试问题也相当烦人,尽管如果您使用 Firebug 之类的工具,当然并非不可能。
调试问题也相当烦人,尽管如果
我不会将它用于静态内容(普通网页),但对于动态更新的内容来说,这无疑是一种合理的方法。
It's not unheard of, but there are issues.
The obvious one is that some users have javascript turned off for security reasons, and they will not be able to use your site at all.
It can also negatively impact handicapped users that are using assistive technology such as a screen reader.
It can make it harder for the browser to effectively cache your static content, slowing down the browsing experience.
It can make it harder for search engines to index your content.
It can cause the back and forward buttons to stop working unless to take special steps to make them work.
It's also fairly annoying to debug problems, although certainly not impossible if you use a tool such as Firebug.
I wouldn't use it for static content (a plain web page) but it's certainly a reasonable approach for content that is dynamically updated anyway.
如果您不需要额外的工作,它就会杀死后退和前进历史按钮,并且很难链接到每个按钮加载的页面。您必须实现某种 URL 更改机制,例如通过在 URL 的哈希中对最后单击的页面进行编码(例如,当您单击按钮时,您将重定向到
#page-2
或其他内容)。它还会使禁用 JavaScript 的用户无法访问您的网站。良好网页设计的原则之一是“优雅降级”——使用 JavaScript、Flash 或 CSS 等高级功能增强您的网站,但即使它们被禁用,它们仍然可以工作。
Without extra work on your part it kills the back and forward history buttons, and it makes it difficult to link to the pages each button loads. You'd have to implement some sort of URL changing mechanism, for example by encoding the last clicked page in the URL's hash (e.g. when you click a button you redirect to
#page-2
or whatever).It also makes your site inaccessible to users with JavaScript disabled. One of the principles of good web design is "graceful degradation"--enhancing your site with advanced features like JavaScript or Flash or CSS but still working if they are disabled.
两个注意事项:搜索引擎优化 (SEO) 和书签。
是否有直接的 URL 可以访问您的标题链接?如果是这样,你(几乎)没问题。例如,以下代码既适合 SEO,又可以根据您的需要填充您的页面:
当人们尝试为他们通过 JavaScript 加载的页面添加书签时,就会出现问题……这种情况不会发生。您可以将大部分潜在的推文、电子邮件推荐和头版 Digg/Reddit 文章扔出窗外。普通用户不知道如何链接到您的内容。
Two considerations: Search engine optimization (SEO) and bookmarks.
Is there a direct URL to access your header links? If so, you're (almost) fine. For example, the following code is both SEO friendly and populates your page as you desire:
The catch occurs when people attempt to bookmark the page they've loaded via JavaScript... it won't happen. You can throw most of those potential tweets, email referrals, and front page Digg/Reddit articles out the window. The average user won't know how to link to your content.
你在哪里读到这是一个坏主意?是否动态填充内容完全取决于要求。然而,在大多数情况下,内容不是随页面一起加载的,但如果您需要动态加载内容,那么这应该不是一个坏主意。
如果您的内容是通过 javascript 加载的,并且用户浏览器上禁用了 javascript,那么绝对这是一个坏主意。
Where did you read it is a bad idea? It purely depends on requirements whether or not content will be populated on-the-fly. In most cases, however, the content is loaded along with the page not on-the-fly but if you need your content on-the-fly, it shouldn't be a bad idea.
If your content is loaded via javascript and javascript is disabled on users' browser then definitely it is a bad idea.
我也想不出有什么不好的原因(除了可能的 SEO),一件事可能是一个好主意,那就是只加载一次数据。即
这应该会降低服务器负载,因此 div 内容仅加载一次。
干杯
I cant think of a bad reason for this either (other than possibly SEO), one thing that would probably be a good idea is to load the data only once. ie
This should keep the server load down so the divs content is only loaded once.
Cheers
这是启用 ajax 的站点中相当标准的行为。
但请记住,需要付出额外的努力:
This is pretty standard behavior in ajax enabled sites.
Keep in mind however that extra effort will be needed to: