Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert external page html into a page html

I'd like to load/insert an external html page into my web page. Example :

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

XXXX

Hope you enjoyed

the XXXX should be replaced by a small script (the smaller as possible) that load a page like http://www.mySite.com/myPageToInsert.html

I found the following code with jquery :

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

I would like the same without using an external javascript library as jquery...

like image 544
Zitun Avatar asked Feb 11 '11 09:02

Zitun


2 Answers

There are 2 solutions for this (2 that I know at least):

  1. Iframe -> this one is not so recommended

  2. Send an ajax request to the desired page.

Here is a small script:

<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>
like image 144
zozo Avatar answered Sep 28 '22 18:09

zozo


Would an iframe fit the bill?

<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

You can set the src attribute of your iframe element using plain old javascript to switch out the page for another

like image 36
Phil Jenkins Avatar answered Sep 28 '22 18:09

Phil Jenkins