-1

ページ全体を更新せずに、Web ページの一部を自動的に更新したい。(Web ページの 1 つの区分とします)。

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>

現在、このコードは Internet Explorer を除くすべてのブラウザで正常に動作しています。(Chrome、Mozilla、Opera、Safari を確認しました)。そのため、Internet Explorer のこのバグを修正するための手助けが少し必要です。どんな助けでも大歓迎です。前もって感謝します。

4

1 に答える 1

2

問題は、最初の IE 条件付きコンパイル スクリプトに「@」文字がないことです。行はend @*/代わりに@end @*/.

ただし、JScript バージョンのブラウザー検出を使用することは適切な方法ではないことに注意してください。よりシンプルで堅牢なアプローチは、使用する標準を直接テストし、レガシー ブラウザーの回避策にフォールバックすることです。例えば:

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.
}
于 2012-12-25T17:12:22.633 に答える