How to dynamically change a web page's title?

前端 未结 19 1875
遇见更好的自我
遇见更好的自我 2020-11-22 08:05

I have a webpage that implements a set of tabs each showing different content. The tab clicks do not refresh the page but hide/unhide contents at the client side.

No

19条回答
  •  生来不讨喜
    2020-11-22 08:39

    I want to say hello from the future :) Things that happened recently:

    1. Google now runs javascript that is on your website1
    2. People now use things like React.js, Ember and Angular to run complex javascript tasks on the page and it's still getting indexed by Google1
    3. you can use html5 history api (pushState, react-router, ember, angular) that allows you to do things like have separate urls for each tab you want to open and Google will index that1

    So to answer your question you can safely change title and other meta tags from javascript (you can also add something like https://prerender.io if you want to support non-Google search engines), just make them accessible as separate urls (otherwise how Google would know that those are different pages to show in search results?). Changing SEO related tags (after user has changed page by clicking on something) is simple:

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

    Just make sure that css and javascript is not blocked in robots.txt, you can use Fetch as Google service in Google Webmaster Tools.

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

提交回复
热议问题