How to dynamically change a web page's title?

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.

Now there is a requirement to change the page title according to the tab selected on the page ( for SEO reasons ). Is this possible? Can someone suggest a solution to dynamically alter the page title via javascript without reloading the page?


Update : as per the comments and reference on SearchEngineLand most web crawlers will index the updated title. Below answer is obsolete. but the code is still applicable.

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.


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

  • Google now runs javascript that is on your website1
  • 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
  • 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


    I can't see how changing the page title via Javascript will help SEO. Most (or all) search bots do not run Javascript and will only read the initially loaded title that is the mark-up.

    If you want to help SEO, then you will need to change the page title in the back-end and serve different versions of the page.

    链接地址: http://www.djcxy.com/p/88856.html

    上一篇: 插入四个空格而不是制表符

    下一篇: 如何动态更改网页的标题?