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

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


当前回答

我使用一个单独的头,并包括它使用php,在头我使用:

<?php
if (!isset($title)){
    $title = "Your Title";
  }
  else {
    $title = $title;
  }
 ?>
<head>
<title><?php echo $title; ?></title>

然后在每一页我使用:

<?php
  $title = "Your Title - Page";
  include( "./header.php" );
?>

其他回答

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

首先把你的标题设置为

my app | description | contact | about us | 

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

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

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

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

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

你可以使用JavaScript。一些机器人,包括谷歌,将执行JavaScript以获得SEO的好处(在SERP中显示正确的标题)。

document.title = "Google will run this JS and show the title in the search results!";

但是,这更复杂,因为您在不刷新页面或更改URL的情况下显示和隐藏选项卡。也许添加一个锚会像其他人所说的那样有所帮助。我可能需要收回我的回答。

有积极效果的文章: http://www.aukseo.co.uk/use-javascript-to-generate-seo-friendly-title-tags-1275/ http://www.ifinity.com.au/2012/10/04/Changing_a_Page_Title_with_Javascript_to_update_a_Google_SERP_Entry

不要总是假设bot不会执行JavaScript。 http://searchengineland.com/tested-googlebot-crawls-javascript-heres-learned-220157 谷歌和其他搜索引擎都知道,索引的最佳结果是实际终端用户将在浏览器中看到的结果,包括JavaScript。

Google mentioned that all js files rendered but in real, I've lost my title and another meta tags which had been provided by Reactjs on this website and actually lost my position on Google! I've searched a lot but it seems that all pages must have pre-rendered or using SSR(Server Side Rendering) to have their SEO-friendly protocole! It expands to Reactjs, Angularjs , etc. For short, Every page that has view page source on browser is indexed by all robots, if it's not probably Google can index but others skip indexing!

为了让任何爬虫程序注意到这个变化,您必须用一个新的标题重新提供页面。通过javascript这样做只会让人类读者受益,爬虫不会执行这些代码。