Jak naslouchat změnám prvku title?

Existuje v JavaScriptu technika, jak naslouchat změnám prvku title?

Odpověď

O 5 let později máme konečně lepší řešení. Použijte MutationObserver!

Ve zkratce:

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

S komentáři:

// 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);

Mutation Observer má také úžasnou podporu prohlížeče: