How to dynamically change a web page's title?

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

    Since search engines ignore most javascript, you will need to make it so that search engines can crawl using the tabs without using Ajax. Make each tab a link with an href that loads the entire page with that tab selected. Then the page can have that title in the tag.

    The onclick event handler can still load the pages via ajax for human viewers.

    To see the pages as most search engines see them, turn off Javascript in your browser, and try to make it so that clicking the tabs will load the page with that tab selected and the correct title.

    If you are loading via ajax and you want to dynamically change the page title with just Javascript, then do:

    document.title = 'Put the new title here';
    

    However, search engines will not see this change made in javascript.

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

    There are many ways you can change the title, the main two, are like so:

    The Questionable Method

    Put a title tag in the HTML (e.g. <title>Hello</title>), then in javascript:

    let title_el = document.querySelector("title");
    
    if(title_el)
        title_el.innerHTML = "World";
    

    The Obviously Correct Method

    The simplest of all is to actually use the method provided by the Document Object Model (DOM)

    document.title = "Hello World";
    

    The former method is generally what you would do to alter tags found in the body of the document. Using this method to modify meta-data tags like those found in the head (like title) is questionable practice at best, is not idiomatic, not very good style to begin with, and might not even be portable. One thing you can be sure of, though, is that it will annoy other developers if they see title.innerHTML = ... in code they are maintaining.

    What you want to go with is the latter method. This property is provided in the DOM Specification specifically for the purpose of, as the name suggests, changing the title.

    Note also that if you are working with XUL, you may want to check that the document has loaded before attempting to set or get the title, as otherwise you are invoking undefined behavior (here be dragons), which is a scary concept in its own right. This may or may not happen via JavaScript, as the docs on the DOM do not necessarily pertain to JavaScript. But XUL is a whole 'nother beast, so I digress.

    Speaking of .innerHTML

    Some good advice to keep in mind would be that using .innerHTML is generally sloppy. Use appendChild instead.

    Although two cases where I still find .innerHTML to be useful include inserting plain text into a small element...

    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;
    }());
    

    ...and clearing out a container...

    container.innerHTML = "";
    // as opposed to... umm... okay, I guess I'm rolling my own
    [...container.childNodes].forEach(function(child){
        container.removeChild(child);
    });
    
    0 讨论(0)
  • 2020-11-22 08:23

    Maybe you can load on your title all the tabs titles in one string, and then once you load one of the tabs change the title via javascript

    ex: at first set your title to

    my app | description | contact | about us | 
    

    once you load one of the tabs run:

    document.title = "my app | tab title";
    
    0 讨论(0)
  • 2020-11-22 08:25

    Modern crawlers are able to parse dynamically-generated content in the DOM, so using document.title = ... is perfectly fine.

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

    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.

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

    The code is
    document.title = 'test'

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