2012-12-25 34 views
-1

我要刷新網頁的一部分自動無需刷新整個頁面。 (說網頁的一個分部)。如何解決與Internet Explorer的Ajax自動刷新?

我有定期調用PHP頁面的腳本。

腳本:

<!----------- ********* AJAX code to auto refresh the part of a webpage************ ---------> 
<script langauge="javascript"> 
    function loadXmlHttp(url, id) { 
    var f = this; 
    if (loadXmlHttp.xmlHttp){ 
     f.xmlHttp = loadXmlHttp.xmlHttp(); 
     f.el = document.getElementById(id); 
     f.xmlHttp.open("GET", url, true); 
     f.xmlHttp.onreadystatechange = function(){f.stateChanged();}; 
     f.xmlHttp.send(null); 
    } else { 
     alert('Your browser does not support AJAX!'); 
    } 
    } 
    loadXmlHttp.xmlHttp = null; 
    loadXmlHttp.re = /^http/.test(window.location.href); 
    /*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it 
    /*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled 
    try {loadXmlHttp.ie = window.ActiveXObject}catch(e){}; 
    end @*/ 
    if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re)) 
    loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method 
    else if (/(object)|(function)/.test(typeof createRequest)) 
    loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others 
    else { 
    loadXmlHttp.xmlHttp = null; 
    // Internet Explorer 5 to 6, includes IE 7+ when local // 
    /*@if(@_jscript_version >= 5) 
    try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};} 
    catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){}} 
    @end @*/ 
    } 
    loadXmlHttp.prototype.stateChanged = function(){ 
    if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re)){ 
     this.el.innerHTML = this.xmlHttp.responseText; 
     if(this.success){ 
     this.success(); 
     } 
    } 
    } 
</script> 
<!----------- ********* AJAX code to auto refresh the part of a webpage ends here************ ---------> 

我所在的地方調用這個函數一個DIV是這樣的:現在

<div class="textad" id="text"></div> 
    <script type="text/javascript"> 
    (function(){ 
     var statsrequest = new loadXmlHttp('display_text_ad.php', 'text'), repeat = arguments.callee; 
     statsrequest.success = function(){setTimeout(repeat, 6000);}; 
    })(); 
    </script> 
</div> 

,該代碼工作正常使用除了IE的所有瀏覽器。 (我已經檢查過Chrome,Mozilla,Opera和Safari)。所以,我需要一些幫助來修復Internet Explorer的這個bug。任何幫助將不勝感激。提前致謝。

+0

它僅適用於第一次工作? –

+0

在此先感謝所有幫助,即時通訊新的StackOverflow和即時消息它!任何想法/建議將不勝感激。請提供簡單的項目。所以任何人都可以對如何解決這個問題有任何想法?有沒有關於如何解決這個奇妙問題的建議?有任何想法嗎? – hakre

+1

@FAngel我試圖調用PHP頁面「display_text_ad.php」。除了IE之外,它對所有瀏覽器都能正常工作。 –

回答

2

的問題是,你最初的IE條件編譯腳本缺少一個「@」字符。 end @*/行應改爲@end @*/

應當指出,然而,使用JScript的版本的瀏覽器檢測是not a good practice。更簡單,更強大的方法是直接測試您要使用的標準,並回退到傳統瀏覽器的解決方法。例如:

if (window.XMLHttpRequest) { 
    // Use native XHR object for modern browsers 
    loadXmlHttp.xmlHttp = new XMLHttpRequest(); 
} 
else if (window.ActiveXObject) { 
    // Use the ActiveX control for legacy browsers 
    loadXmlHttp.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
else { 
    // No XHR mechanism is available. 
} 
+0

另外,編碼可能有問題:http://forum.jquery.com/topic/the-problem-with-ie8-and-encoding-error-c00ce56e(如果沒有'Content-type',我得到這個錯誤指定) –

相關問題