簡體   English   中英

將外部頁面html插入頁面html

[英]Insert external page html into a page 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 ...

有兩個解決方案(至少我知道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>

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設置iframe元素的src屬性,以便將頁面切換為另一個頁面

我認為您正在尋找的是Jquery源代碼。

你可以在這里看到更多的細節$(document).ready等效沒有jQuery

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM