AJAX 另一種在html中include一個檔案的方法

來源:互聯網
上載者:User
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function LoadHttp(myName, callback, uri){
with (this)
{
 if(!document.readyState)
  document.readyState = "completed";
 this.myName = myName;
 this.callback = callback;  // 'iframe' is our fallback: a reference to a dynamically created IFRAME buffer.
 this.iframe = null;
 window._ifr_buf_count |= 0;
 this.iframeID = 'iframebuffer' + window._ifr_buf_count++;
 // A loading flag, set to the requested URI when loading.
 this.loadingURI = '';  // In MSIE or browsers with no XMLHttpRequest support: fallback to IFRAMEs buffers.
 // I'm using IFRAMEs in MSIE due to XMLHTTP not parsing text/html to a DOM.
 // Also used in IE5/Mac, Opera 7.x, Safari <1.2, some Konqueror versions, etc.
 if (document.createElement && document.documentElement &&
    (window.opera || navigator.userAgent.indexOf('MSIE 5.0') == -1))
 {
  var ifr = document.createElement('iframe');
  ifr.setAttribute('id', iframeID);
  ifr.setAttribute('name', iframeID);
  // Hide with visibility instead of display to fix Safari bug.
  ifr.style.visibility = 'hidden';
  ifr.style.position = 'absolute';
  ifr.style.width = ifr.style.height = ifr.borderWidth = '0px';
  iframe = document.getElementsByTagName('body')[0].appendChild(ifr);
 }
 else if (document.body && document.body.insertAdjacentHTML)
 {
  // IE5.0 doesn't like createElement'd frames (won't script them) and IE4 just plain
  // doesn't support it. Luckily, this will fix them both:
  document.body.insertAdjacentHTML('beforeEnd', '<iframe name="' + iframeID +
   '" id="' + iframeID + '" style="display: none"></iframe>');
 }
 // This helps most IE versions regardless of the creation method:
 if (window.frames && window.frames[iframeID]) iframe = window.frames[iframeID];
 iframe.name = iframeID;
}
this.load(uri);
// TODO: Put in further DOM3 LSParser support as a transport layer...?
return this;
}; LoadHttp.prototype.iframeSend = function(uri, formRef)
{
 with (this)
 {
  // Routes a request through our hidden IFRAME. Pass a URI, and optionally a
  // reference to a submitting form. Requires proprietary 'readyState' property.   if (!document.readyState)
   return false;   // Opera fix: force the frame to render before setting it as a target.
  if (document.getElementById)
   var o = document.getElementById(iframeID).offsetWidth;   // Either route the form submission to the IFRAME (easy!)...
  if (formRef)
   formRef.setAttribute('target', iframeID);
  else
  {
   // ...or load the provided URI in the IFRAME, checking for browser bugs:
   // 1) Safari only works well using 'document.location'.
   // 2) Opera needs the 'src' explicitly set!
   // 3) Konqueror 3.1 seems to think ifrDoc's location = window.location, so watch that too.
   var ifrDoc = iframe.contentDocument || iframe.document;    if (!window.opera && ifrDoc.location && ifrDoc.location.href != location.href)
    ifrDoc.location.replace(uri);
   else
    iframe.src = uri;
  }   // Either way, set the loading flag and start the readyState checking loop.
  // Opera likes a longer initial timeout with multiple frames running...
  loadingURI = uri;   setTimeout(myName + '.iframeCheck()', (window.opera ? 250 : 100));
  return true;
 }
}; LoadHttp.prototype.iframeCheck = function()
{
 with (this)
 {
  // Called after a timeout, periodically calls itself until the load is complete.
  // Get a reference to the loaded document, using either W3 contentDocument or IE's DOM.
  
  doc = iframe.contentDocument || iframe.document;
  // Check the IFRAME's .readyState property and callback() if load complete.
  // IE4 only seems to advance to 'interactive' so let it proceed from there.
  var il = iframe.location, dr = !dr || doc.readyState;
  if ((il && il.href ? il.href.match(loadingURI) : 1) &&
   (dr == 'complete' || (!document.getElementById && dr == 'interactive')
   || doc.documentElement.innerHTML != "<head></head><body></body>"))
  {
   if (callback)
    callback((doc.documentElement || doc), loadingURI);
   loadingURI = '';
  }
  else
  {
   alert(1);
   setTimeout(myName + '.iframeCheck()', 50);
   }
 }
}; // *** PUBLIC METHODS ***
LoadHttp.prototype.load = function(uri)
{
 with (this)
 {
  // Call with a URI to load a plain text document.   if (!uri || !iframe)
   return false;
  // Route the GET through an available transport layer.
  if (iframe)
   return iframeSend(uri, null);
  else
   return false;
 }
};
//-->
</SCRIPT> </HEAD>
<BODY>
<span id="showitem"></span>
</BODY>
<SCRIPT LANGUAGE=javascript>
<!--
function onloadshowitem(content, uri)
{
// alert(content.innerHTML);
 showitem.innerHTML = content.innerHTML;
}
var lhp = new LoadHttp("lhp", onloadshowitem,"hello.htm");
//-->
</SCRIPT>
</HTML>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.