我有一个网页,实现了一组选项卡每个显示不同的内容。单击选项卡不会刷新页面,而是隐藏/取消隐藏客户端的内容。

现在需要根据页面上选择的选项卡更改页面标题(出于SEO原因)。这可能吗?有人能建议一个解决方案,通过javascript动态改变页面标题,而不重新加载页面?


当前回答

我想从未来向你问好:)最近发生的事情:

谷歌现在运行javascript,是在你的网站1 人们现在使用React.js、Ember和Angular在页面上运行复杂的javascript任务,而且它仍然被Google1索引 你可以使用html5历史api (pushState, react-router, ember, angular),它允许你做一些事情,比如为你想打开的每个选项卡设置单独的url,谷歌将索引that1

所以为了回答你的问题,你可以安全地从javascript中更改标题和其他元标签(如果你想支持非谷歌搜索引擎,你也可以添加一些像https://prerender.io这样的东西),只是让它们作为单独的url访问(否则谷歌如何知道这些是要在搜索结果中显示的不同页面?)更改SEO相关标签(在用户通过点击更改页面后)很简单:

if (document.title != newTitle) {
    document.title = newTitle;
}
$('meta[name="description"]').attr("content", newDescription);

只要确保css和javascript没有在robots.txt中被阻止,你可以在谷歌网站管理员工具中使用Fetch作为谷歌服务。

1: http://searchengineland.com/tested-googlebot-crawls-javascript-heres-learned-220157

其他回答

我想从未来向你问好:)最近发生的事情:

谷歌现在运行javascript,是在你的网站1 人们现在使用React.js、Ember和Angular在页面上运行复杂的javascript任务,而且它仍然被Google1索引 你可以使用html5历史api (pushState, react-router, ember, angular),它允许你做一些事情,比如为你想打开的每个选项卡设置单独的url,谷歌将索引that1

所以为了回答你的问题,你可以安全地从javascript中更改标题和其他元标签(如果你想支持非谷歌搜索引擎,你也可以添加一些像https://prerender.io这样的东西),只是让它们作为单独的url访问(否则谷歌如何知道这些是要在搜索结果中显示的不同页面?)更改SEO相关标签(在用户通过点击更改页面后)很简单:

if (document.title != newTitle) {
    document.title = newTitle;
}
$('meta[name="description"]').attr("content", newDescription);

只要确保css和javascript没有在robots.txt中被阻止,你可以在谷歌网站管理员工具中使用Fetch作为谷歌服务。

1: http://searchengineland.com/tested-googlebot-crawls-javascript-heres-learned-220157

也许你可以在一个字符串中加载你的标题上所有的选项卡标题,然后一旦你加载其中一个选项卡,通过javascript改变标题

首先把你的标题设置为

my app | description | contact | about us | 

一旦你加载一个标签运行:

document.title = "my app | tab title";

由于搜索引擎忽略了大多数javascript,因此您需要使搜索引擎能够使用标签而不使用Ajax进行抓取。使用href将每个选项卡设置为链接,以加载选中该选项卡的整个页面。然后页面可以在标签中包含该标题。

onclick事件处理程序仍然可以通过ajax为人类查看者加载页面。

要像大多数搜索引擎看到的那样查看页面,请关闭浏览器中的Javascript,并尝试使其具有这样的功能,即单击选项卡将加载选中的选项卡和正确的标题的页面。

如果你通过ajax加载,你想用Javascript动态地改变页面标题,那么做:

document.title = 'Put the new title here';

但是,搜索引擎不会看到javascript中所做的这个更改。

使用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>

想到的一种方法,可能有助于搜索引擎优化,仍然有你的标签页,因为他们将使用命名锚对应于每个标签,如:

http://www.example.com/mypage#tab1, http://www.example.com/mypage#tab2等。

您需要服务器端处理来解析url,并在浏览器呈现页面时设置初始页面标题。我还会把这个标签设置为“活动”标签。一旦页面被加载,一个实际的用户正在切换标签,你将使用javascript来改变文档。标题如其他用户所述。