锚定 AJAX 和 SEO 解决方法?
你们都知道如何使用 300 毫秒触发器来构建 AJAX 站点,用于检查 URL 中的锚点(哈希链接),然后使用 AJAX 加载正确的页面。但是,这些锚链接对搜索引擎来说毫无意义=(
我想过采取某种解决方法。所有JS代码保持不变,但是,这个小东西(我使用JQuery,抱歉):
$('a').live("click",function(){
var lnk = $(this).attr("href");
document.location.hash = lnk;
return false;
})
然后,你替换您在正文中使用纯链接锚定链接,为非 javascript 用户和搜索引擎构建相应的纯页面(仍包含所有 JS 代码),对于普通访问者,您会将纯链接即时转换为哈希值并立即加载 AJAX 内容。那些试图加载通过搜索引擎找到的某些页面的人 - 他们会的,之后访问者将继续使用 ajax 导航......以某种方式(记住,那些简单的直接页面仍然包含 JS 代码)
。确定我的假设是正确的吗?
更新: 不好的是,当用户直接输入 /portfolio 地址时,他会继续< em>/portfolio#contacts 或类似的 URL,虽然不太漂亮,但仍然有效(我的意思是 /portfolio#contacts 会显示联系人)。
You all know how to build AJAX sites with those 300ms trigger for checking anchors (hash links) in URL and then loading proper page with AJAX. But, these anchor links are nothing to search engines =(
I've thought of making some kind of workaround. All JS code remains the same, but, this little thing (I'm with JQuery, sorry):
$('a').live("click",function(){
var lnk = $(this).attr("href");
document.location.hash = lnk;
return false;
})
And then, you replace your anchor links in the body with plain links, build corresponding plain pages (still containing all JS codes) for non-javascript users and search engines. For normal visitors you would have plain links converted into hashes on fly and AJAX content loaded immediately. For those who are trying to load certain pages found through search engine - they will, and after that visitor will continue to move around with ajax navigation... somehow (remember, those plain direct pages still contain JS code).
I just want to make sure my assumptions are right. Are they?
Update: Bad thing is that when user directly enters some internal page with for ex. /portfolio address, he would then continue to /portfolio#contacts or similar URLs that are not so pretty, but still working (I mean /portfolio#contacts would show contacts).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎不是一个好的方法,主要是因为您最终会为每个链接的
click
事件触发该函数,即使是那些您不这样做的链接 em> 希望使用 AJAX 方法加载内容,例如指向其他站点或javascript:...
的链接。如果您将所有 AJAX-y 链接放在同一个类下或使用另一个属性来区分它们,然后调整您在那里使用的 jQuery 选择器,那么它将起作用,并且这将是一个很好的方法,因为它不仅SEO 友好,但它也提供优雅的降级,也就是说,即使您的用户在不允许 JavaScript 的环境中运行,他们仍然能够访问内容。
除此之外,如果 SEO 是您唯一关心的问题,您可以在网络上搜索
sitemap.xml
文件是什么以及如何使用它。维基百科有一篇关于它的文章可能值得一读。That doesn't seem to be a good approach mainly because you'll end up having that function being triggered for the
click
event of each and every link, even those where you do not want to use your AJAX approach for loading content, for instance links to other sites or tojavascript:...
.If you place all your AJAX-y links under the same class or use another attribute to distinguish them and then adapt the jQuery selector that you're using there, then it will work and it will be a good approach because not only will it be SEO-friendly, but it also provides graceful degradation, that is, your users will still be able to access the content even if they're running in an environment where no JavaScript is allowed.
Other than that, and if SEO is your only concern, you may search the web for what a
sitemap.xml
file is and how to use it. Wikipedia has an article on it that may be worth a reading.听起来很不错...只是我建议您的 300 毫秒计时器上的回调也可以在上面的事件中调用,这样您在单击链接时就会立即得到结果(并且用户也可以转到地址栏中的链接)并获得相同的效果)
Sounds very sound... only I would suggest the callback on your 300ms timer also be called inside the event you have above, that way you have an instant result when clicking a link(and also a user can goto the link in the address bar and get the same effect)
嘿,伙计,您可能想看看 jQuery Ajaxy。它将网站优雅地升级为丰富的 ajax 应用程序,因此您的网站仍然适用于 SEO 和 Javascript 禁用的用户。
您可以在此即将推出的网站中看到此功能的实际应用。请注意,所有链接与网站不是 Ajax 应用程序时的链接相同 - 并且如果您右键单击并在新窗口中打开,它们仍然有效!
此功能在其演示页面中有详细解释。您还可以在那里找到其他示例和使用说明:
http://www.balupton.com/sandbox/jquery-ajaxy/demo/< /a>
总体而言,它的使用非常简单明了(特别是考虑到它允许您做什么!),而且支持非常棒。
Hey mate, you probably want to check out jQuery Ajaxy. It gracefully upgrades websites into rich ajax applications, so your website still works for SEO and Javascript disabled users.
You can see this functionality in action in this upcoming website. Notice how all the links are the same as they would be if the website isn't a Ajax application - and they still work if you right click and go open in new window!
This functionality is explained in detail at it's demo page. You can also find other examples and usage instructions there to:
http://www.balupton.com/sandbox/jquery-ajaxy/demo/
Overall to use it is surprisingly simple and straightforward (especially considering what it allows you to do!), and the support is fantastic.