How to dynamically change a web page's title?

前端 未结 19 1442
遇见更好的自我
遇见更好的自我 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:30

    You can use JavaScript. Some bots, including Google, will execute the JavaScript for the benefit of SEO (showing the correct title in the SERP).

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

    However, this is more complex since you are showing and hiding tabs without refreshing the page or changing the URL. Maybe adding an anchor will help as stated by others. I may need to retract my answer.

    Articles showing positive results: 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

    Don't always assume a bot won't execute JavaScript. http://searchengineland.com/tested-googlebot-crawls-javascript-heres-learned-220157 Google and other search engines know that the best results to index are the results that the actual end user will see in their browser, including JavaScript.

    0 讨论(0)
  • 2020-11-22 08:33

    But in order to get the SEO befits

    You need to do a page reload when the page changes so that the search engine's see the different titles etc.

    So make sure the page reload works first then add document.title changes

    0 讨论(0)
  • 2020-11-22 08:37

    One way that comes to mind that may help with SEO and still have your tab pages as they are would be to use named anchors that correspond to each tab, as in:

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

    You would need to have server side processing to parse the url and set the initial page title when the browser renders the page. I would also go ahead and make that tab the "active" one. Once the page is loaded and an actual user is switching tabs you would use javascript to change document.title as other users have stated.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 08:39

    for those looking of the npm version of it, there is an entire library for this:

    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'));
    
    0 讨论(0)
  • 2020-11-22 08:40

    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.

    0 讨论(0)
提交回复
热议问题