在散列更改时,文档标题在ie8中发生变化

时间:2013-07-04 09:56:18

标签: javascript

有没有办法防止ie8中的文档标题更改。因为它会根据url中的哈希值自动更改(www。#hash)

例如:考虑我的标题是:

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

如果哈希变为#691,则IE8中的标题变为$ 691:

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

更好的解决方案
$(window).load(function(){
    setTimeout(function(){
        window.document.title = "Some title";
    }, 1000);
});

3 个答案:

答案 0 :(得分:1)

我找到了一个适用于IE9和更高版本的更好解决方案IE11。 在现代浏览器中,您可以使用通用DOMSubtreeModified事件,但是在较旧的浏览器中,我使用了onpropertychange事件,该事件仅与传统的attachEvent IE-only事件注册模型一起支持,该模型自Windows Internet Explorer 9以来已弃用, W3C标准“addEventListener”事件模型。

在IE9&amp; 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;
          }
        }
      };
    }
  };
}

答案 1 :(得分:0)

看起来像IE的问题(特定于嵌入了 swf(flash / flex)的页面),请查看以下链接

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

Heikki

提供的答案

答案 2 :(得分:0)

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