jedierikb jedierikb - 1 year ago 86
Javascript Question

how to listen for changes to the title element?

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

Answer Source

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.


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 =;
            if (t === titleEl || (t.parentNode && t.parentNode === titleEl)) {
        }, false);
    } else {
        document.onpropertychange = function() {
            if (window.event.propertyName == "title") {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download