Load HTML File Contents to Div [without the use of iframes]

JavascriptHtml

Javascript Problem Overview


I'm quite sure this a common question, but I'm pretty new to JS and am having some trouble with this.

I would like to load x.html into a div with id "y" without using iframes. I've tried a few things, searched around, but I can't find a decent solution to my issue.

I would prefer something in JavaScript if possible.

Thanks in advance, everyone!

Javascript Solutions


Solution 1 - Javascript

Wow, from all the framework-promotional answers you'd think this was something JavaScript made incredibly difficult. It isn't really.

var xhr= new XMLHttpRequest();
xhr.open('GET', 'x.html', true);
xhr.onreadystatechange= function() {
    if (this.readyState!==4) return;
    if (this.status!==200) return; // or whatever error handling you want
    document.getElementById('y').innerHTML= this.responseText;
};
xhr.send();

If you need IE<8 compatibility, do this first to bring those browsers up to speed:

if (!window.XMLHttpRequest && 'ActiveXObject' in window) {
    window.XMLHttpRequest= function() {
        return new ActiveXObject('MSXML2.XMLHttp');
    };
}

Note that loading content into the page with scripts will make that content invisible to clients without JavaScript available, such as search engines. Use with care, and consider server-side includes if all you want is to put data in a common shared file.

Solution 2 - Javascript

jQuery .load() method:

$("#y").load("x.html");

Solution 3 - Javascript

2019
Using fetch

<script>
fetch('page.html')
  .then(response=> response.text())
  .then(text=> document.getElementById('elementID').innerHTML = text);
</script>

<div id='elementID'> </div>

fetch needs to receive a http or https link, this means that it won't work locally.

Note: As Altimus Prime said, it is a feature for modern browsers

Solution 4 - Javascript

I'd suggest getting into one of the JS libraries out there. They ensure compatibility so you can get up and running really fast. jQuery and DOJO are both really great. To do what you're trying to do in jQuery, for example, it would go something like this:

<script type="text/javascript" language="JavaScript">
$.ajax({
    url: "x.html", 
    context: document.body,
    success: function(response) {
        $("#yourDiv").html(response);
    }
});
</script>

Solution 5 - Javascript

    document.getElementById("id").innerHTML='<object type="text/html" data="x.html"></object>';

Solution 6 - Javascript

2021

Two possible changes to thiagola92's answer.

  1. async await - if preferred

  2. insertAdjacentHTML over innerText (faster)

    <script>
    async function loadHtml() {
         const response = await fetch("page.html")
         const text = await response.text()
         document.getElementById('elementID').insertAdjacentText('beforeend', text)
    }
    
    loadHtml()
    </script>
    <!-- ... -->
    <div id='elementID'> </div>
    

Solution 7 - Javascript

http://www.boutell.com/newfaq/creating/include.html

this would explain how to write your own clientsideinlcude but jQuery is a lot, A LOT easier option ... plus you will gain a lot more by using jQuery anyways

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMapWebView Question on Stackoverflow
Solution 1 - JavascriptbobinceView Answer on Stackoverflow
Solution 2 - JavascriptDejan MarjanovićView Answer on Stackoverflow
Solution 3 - Javascriptthiagola92View Answer on Stackoverflow
Solution 4 - JavascriptKeatsKelleherView Answer on Stackoverflow
Solution 5 - Javascriptjohn pView Answer on Stackoverflow
Solution 6 - JavascriptMartin MeeserView Answer on Stackoverflow
Solution 7 - JavascriptdrusnovView Answer on Stackoverflow