DIV CSS 佈局教程網

用 JavaScript 取得遠端網頁源碼
編輯:關於JavaScript     

瀏覽器可透過 XMLHttpRequest 向伺服器發出 GET 請求,取得遠端網頁內容。不過若目標網域與目前頁面不同,需留意同源政策限制。

以下範例在請求完成後,將 responseText 寫入 textarea。

function getSource(url) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      document.getElementById('source').value = xhr.responseText;
    }
  };
  xhr.send(null);
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved