MapWeb
MapWeb

Reputation: 775

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

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.

Upvotes: 71

Views: 164620

Answers (8)

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

Upvotes: 31

connexo
connexo

Reputation: 56744

There was a way to achieve this in the past, but it was removed from the specification, and subsequently, from browsers as well (e.g. Chrome removed it in Chrome 70). It was called HTML imports and it originally was part of the web components specs.

Currently folks are working on a replacement for this obviously lacking platform feature, which will be called HTML modules. Here's the explainer, and here's the Chrome platform status for this feature. There is no milestone specified yet as of when this feature will land.

Chances are the syntax is going to look similar to this:

import { content } from "file.html";

Resolving the remaining issues with HTML modules I assume might take quite some time, so until then the only viable options you have is to have

We already have JSON modules and CSS module scripts (which both were sorely missing features for a long time as well).

Upvotes: 1

Martin Meeser
Martin Meeser

Reputation: 2956

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>
    

Upvotes: 12

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

jQuery .load() method:

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

Upvotes: 81

KeatsKelleher
KeatsKelleher

Reputation: 10191

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>

Upvotes: 7

john p
john p

Reputation: 132

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

Upvotes: 6

bobince
bobince

Reputation: 536349

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.

Upvotes: 117

drusnov
drusnov

Reputation: 363

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

Upvotes: -1

Related Questions