Vardhaman
Vardhaman

Reputation: 11

document title is changing in ie8 on hash change

Is there a way to prevent document title change in ie8. because it is changing automatically based on the hash in url (www.#hash)

for example: consider my title is:

<head><title>Welcome</title></head>

if the hash changes to #691 the title become $691 in IE8:

<head><title>#691</title></head>

any better solution than

$(window).load(function(){
    setTimeout(function(){
        window.document.title = "Some title";
    }, 1000);
});

Upvotes: 1

Views: 1113

Answers (3)

momofiona
momofiona

Reputation: 1

if (browser.ie < 10) {
    document.attachEvent('onpropertychange', function(evt) {
        if (evt.propertyName === 'title' && document.title) {
            setTimeout(function() {
                var b=document.title.indexOf('#');
                if(b!==-1){
                    document.title = document.title.slice(0,b);
                }

            }, 1000);
        }
    });
}

Upvotes: 0

Itay Radotzki
Itay Radotzki

Reputation: 857

I found a better solution that works on IE9 & IE11. In modern browsers you can use the generic DOMSubtreeModified event, however In older browsers I used the onpropertychange event, which is only supported in conjunction with the legacy attachEvent IE-only event registration model, which has deprecated since Windows Internet Explorer 9 in favor of the W3C standard "addEventListener" event model.

Tested on IE9 & IE11

function handleIETitle() {
  var originalTitle = document.title.split('#')[0];

  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)) {
          if (document.title !== originalTitle) {
            document.title = originalTitle;
          }
        }
      }, false);
    } else {
      document.onpropertychange = function () {
        if (window.event.propertyName === 'title') {
          if (document.title !== originalTitle) {
            document.title = originalTitle;
          }
        }
      };
    }
  };
}

Upvotes: 2

Gnanz
Gnanz

Reputation: 1873

It is look like an issue with IE(specific to the page that has swf(flash/flex) embedded), check the below links

IE title changes to <afterHash> if the page has a url with '#' , and has flash/swf embedded in it

the answer provided by Heikki

Upvotes: 1

Related Questions