Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen for changes to the title element?

In Javascript, is there a technique to listen for changes to the title element?

like image 698
jedierikb Avatar asked Mar 23 '10 02:03

jedierikb


People also ask

How do you change the title of a document?

Go to File > Info > Properties > Title. Click “Add a title” and paste your Heading 1 into the textbox. Or, type in an easy-to-read title for your document.

How do I get page title in browser console?

Put in the URL bar and then click enter: javascript:alert(document. title);

How do I change the name of a document in HTML?

To set the title for an HTML Document, use the HTML <title> tag. The body title is placed between the <head> and the </head> tags. HTML document title is visible on the web browsers title bar. It is considered good for SEO.

How do I change the name of a document in JavaScript?

Use the querySelector() Method to Change the Page Title in JavaScript. We can use the document. querySelector() method to pick elements in a document. The title element can be chosen by giving the title element a selector parameter and retrieving the page's main title element.


2 Answers

5 years later we finally have a better solution. Use MutationObserver!

In short:

new MutationObserver(function(mutations) {     console.log(mutations[0].target.nodeValue); }).observe(     document.querySelector('title'),     { subtree: true, characterData: true, childList: true } ); 

With comments:

// select the target node var target = document.querySelector('title');  // create an observer instance var observer = new MutationObserver(function(mutations) {     // We need only first event and only new value of the title     console.log(mutations[0].target.nodeValue); });  // configuration of the observer: var config = { subtree: true, characterData: true, childList: true };  // pass in the target node, as well as the observer options observer.observe(target, config); 

Also Mutation Observer has awesome browser support:

like image 164
Vladimir Starkov Avatar answered Sep 20 '22 08:09

Vladimir Starkov


2022 Update

Mutation Observers are unequivocally the way to go now (see Vladimir Starkov's answer), with no need for fallbacks to the older APIs mentioned below. Furthermore, DOMSubtreeModified should be actively avoided now.

I'm leaving the remainder of this answer here for posterity.

2010 Answer

You can do this with events in most modern browsers (notable exceptions being all versions of Opera and Firefox 2.0 and earlier). In IE you can use the propertychange event of document and in recent Mozilla and WebKit browsers you can use the generic DOMSubtreeModified event. For other browsers, you will have to fall back to polling document.title.

Note that I haven't been able to test this in all browsers, so you should test this carefully before using it.

2015 Update

Mutation Observers are the way to go in most browsers these days. See Vladimir Starkov's answer for an example. You may well want some of the following as fallback for older browsers such as IE <= 10 and older Android browsers.

function titleModified() {
    window.alert("Title modifed");
}

window.onload = function() {
    var titleEl = document.getElementsByTagName("title")[0];
    var docEl = document.documentElement;

    if (docEl && docEl.addEventListener) {
        docEl.addEventListener("DOMSubtreeModified", function(evt) {
            var t = evt.target;
            if (t === titleEl || (t.parentNode && t.parentNode === titleEl)) {
                titleModified();
            }
        }, false);
    } else {
        document.onpropertychange = function() {
            if (window.event.propertyName == "title") {
                titleModified();
            }
        };
    }
};
like image 24
Tim Down Avatar answered Sep 17 '22 08:09

Tim Down