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

现在需要根据页面上选择的选项卡更改页面标题(出于SEO原因)。这可能吗?有人能建议一个解决方案,通过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!

其他回答

有很多方法可以改变标题,主要有两种:

可疑的方法

在HTML中放入title标签(例如<title>Hello</title>),然后在javascript中:

let title_el = document.querySelector("title");

if(title_el)
    title_el.innerHTML = "World";

明显正确的方法

最简单的方法是实际使用文档对象模型(DOM)提供的方法

document.title = "Hello World";

前一种方法通常用于更改文档正文中的标记。使用这种方法来修改元数据标签,就像在头部(如title)中发现的那样,这是一种值得怀疑的做法,不是惯用的,不是很好的风格,甚至可能无法移植。但你可以肯定的一点是,如果其他开发者看到你的标题,他们会很恼火。innerHTML =…他们维护的代码。

你想要的是后一种方法。这个属性是在DOM规范中提供的,顾名思义,专门用于更改标题。

还要注意,如果使用XUL,可能希望在尝试设置或获取标题之前检查文档是否已加载,否则将调用未定义的行为(这里是龙),这本身就是一个可怕的概念。这可以通过JavaScript实现,也可以不实现,因为DOM上的文档不一定属于JavaScript。但是XUL是完全不同的东西,所以我离题了。

说到.innerHTML

要记住的一些好建议是,使用. innerhtml通常是草率的。请改用appendChild。

虽然在两种情况下,我仍然发现.innerHTML是有用的,包括插入纯文本到一个小元素…

label.innerHTML = "Hello World";
// as opposed to... 
label.appendChild(document.createTextNode("Hello World"));
// example:
el.appendChild(function(){
    let el = document.createElement("span");
    el.className = "label";
    el.innerHTML = label_text;
    return el;
}());

...清理一个集装箱…

container.innerHTML = "";
// as opposed to... umm... okay, I guess I'm rolling my own
[...container.childNodes].forEach(function(child){
    container.removeChild(child);
});

现代爬虫程序能够解析DOM中动态生成的内容,因此使用document。标题=…完全没问题。

对于那些寻找它的NPM版本的人,有一个完整的库:

npm install --save react-document-meta

import React from 'react';
import DocumentMeta from 'react-document-meta';

class Example extends React.Component {
  render() {
    const meta = {
      title: 'Some Meta Title',
      description: 'I am a description, and I can create multiple tags',
      canonical: 'http://example.com/path/to/page',
      meta: {
        charset: 'utf-8',
        name: {
          keywords: 'react,meta,document,html,tags'
        }
      }
    };

    return (
      <div>
        <DocumentMeta {...meta} />
        <h1>Hello World!</h1>
      </div>
    );
  }
}

React.render(<Example />, document.getElementById('root'));

为了让任何爬虫程序注意到这个变化,您必须用一个新的标题重新提供页面。通过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>