2011-02-11 91 views
6

我想將外部html頁面加載/插入到我的網頁中。例如:將外部頁面html插入頁面html

<b>Hello this is my webpage</b> 
You can see here an interresting information : 

XXXX 

Hope you enjoyed 

XXXX應該由一個小的腳本替換(在儘可能小),該加載頁一樣http://www.mySite.com/myPageToInsert.html

我發現與jquery以下代碼:

<script>$("#testLoad").load("http://www.mySite.com/myPageToInsert.html");</script>  
<div id="testLoad"></div> 

我想同樣沒有使用外部JavaScript庫作爲jQuery ...

回答

7

這有(2,我至少知道)2級的解決方案:

  1. 的Iframe - >這個人是不是如此建議

  2. 發送ajax請求到所需的頁面。

這裏是一個小腳本:

<script type="text/javascript"> 

function createRequestObject() { 
    var obj; 
    var browser = navigator.appName; 
    if (browser == "Microsoft Internet Explorer") { 
     obj = new ActiveXObject("Microsoft.XMLHTTP"); 
    } else { 
     obj = new XMLHttpRequest(); 
    } 
    return obj; 
} 

function sendReq(req) { 
    var http = createRequestObject(); 
    http.open('get', req); 
    http.onreadystatechange = handleResponse; 
    http.send(null); 
} 

function handleResponse() {  
    if (http.readyState == 4) { 
     var response = http.responseText; 
     document.getElementById('setADivWithAnIDWhereYouWantIt').innerHTML=response; 
    } 
} 

sendReq('yourpage'); 
//previously </script> was not visible 
</script> 
+0

優秀的,這是一個... thx – Zitun 2011-02-11 10:13:07

4

iframe符合法案?

<b>Hello this is my webpage</b> 
You can see here an interresting information : 

<iframe id="extFrame" src="http://www.mySite.com/myPageToInsert.html"></iframe> 

Hope you enjoyed 

可以使用普通的舊JavaScript來切換出頁面,另一個

+0

THX,我也沒多想IFRAME,因爲它有時在一些網站上有問題...但我應該重新考慮它。 – Zitun 2011-02-11 10:07:34