2013-07-04 72 views
0

有沒有辦法來阻止在ie8文件標題的變化。因爲它正在改變自動根據url中的哈希(WWW#哈希)文件標題正在改變在ie8上哈希變化

例如:考慮我的標題是:

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

如果哈希變化#691標題在IE8成爲$ 691:

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

比任何

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

我不能重複這種行爲 – raam86

+0

你嘗試過什麼?修正你的HTML可能會訣竅,因爲這有味道的湯。嘗試通過使用HTML5文檔類型('<!DOCTYPE html>')強制IE8進入(接近)標準 –

+0

還有一些選項如何在IE8的標題欄中顯示頁面標題,但我無法記得任何正確的術語爲名。請檢查互聯網選項>高級,那裏是... – Teemu

回答

1

提供的答案,我找到了一個更好的解決方案,在IE9工作& IE11。 在現代瀏覽器中,您可以使用通用的DOMSubtreeModified事件,但是在較早的瀏覽器中,我使用了onpropertychange事件,該事件僅支持與傳統attachEvent IE-only事件註冊模型結合使用,該模型自Windows Internet Explorer 9以來不推薦使用, W3C標準的「addEventListener」事件模型。

測試在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; 
 
      } 
 
     } 
 
     }; 
 
    } 
 
    }; 
 
}

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