我有一个网页,实现了一组选项卡每个显示不同的内容。单击选项卡不会刷新页面,而是隐藏/取消隐藏客户端的内容。
现在需要根据页面上选择的选项卡更改页面标题(出于SEO原因)。这可能吗?有人能建议一个解决方案,通过javascript动态改变页面标题,而不重新加载页面?
我有一个网页,实现了一组选项卡每个显示不同的内容。单击选项卡不会刷新页面,而是隐藏/取消隐藏客户端的内容。
现在需要根据页面上选择的选项卡更改页面标题(出于SEO原因)。这可能吗?有人能建议一个解决方案,通过javascript动态改变页面标题,而不重新加载页面?
当前回答
现代爬虫程序能够解析DOM中动态生成的内容,因此使用document。标题=…完全没问题。
其他回答
更新:根据SearchEngineLand上的评论和参考 大多数网络爬虫将索引更新的标题。下面的答案是过时的,但代码仍然适用。
You can just do something like, document.title = "This is the new page title.";, but that would totally defeat the purpose of SEO. Most crawlers aren't going to support javascript in the first place, so they will take whatever is in the element as the page title. If you want this to be compatible with most of the important crawlers, you're going to need to actually change the title tag itself, which would involve reloading the page (PHP, or the like). You're not going to be able to get around that, if you want to change the page title in a way that a crawler can see.
现代爬虫程序能够解析DOM中动态生成的内容,因此使用document。标题=…完全没问题。
使用document.title。它对大多数事情都很有用,但它会破坏你网站上的SEO。
例子:
document.write("title - " + document.title + "<br>"); document.title = "New title here!"; // Notice: this will defeat purpose of SEO. Not useful for SEO-friendly sites. document.write("title - " + document.title + "<br>"); body { font-family: Consolas, 'Courier New', monospace; } <!DOCTYPE html> <html> <head><title>Old title</title></head> <body><p> Lorem ipsum dolor sit amet, at movet detraxit mediocritatem eam, nam iusto abhorreant ne. Ei pro debet adolescens voluptaria, eu minim scaevola conceptam vel. Vim ea torquatos constituto complectitur, usu eu civibus insolens eleifend. Ex ubique quaerendum his. </p></body> </html>
使用文档。标题是如何在JavaScript中完成它,但这应该如何协助SEO?机器人通常不会在遍历页面时执行javascript代码。
我只是想在这里添加一些东西:如果您通过AJAX更新数据库,通过JavaScript更改标题实际上是有用的,因此标题更改而无需刷新页面。标题实际上是通过你的服务器端脚本语言改变的,但通过JavaScript改变它只是一个可用性和UI方面的事情,让用户体验更加愉快和流畅。
现在,如果你只是为了好玩而通过JavaScript更改标题,那么你不应该这样做。