How to load HTML file from another HTML file using javascript and not JQuery?
I have seen couple of examples using jquery .load function but I need to know how can we load html file using simple javascript.
How to load HTML file from another HTML file using javascript and not JQuery?
I have seen couple of examples using jquery .load function but I need to know how can we load html file using simple javascript.
jQuery load function is a simple wrapper around XMLHttpRequest
object that loads content using the specified url and places it into the matched element. So, your example $("head").load("sample.html");
loads the content of the sample.html page and places it into the head
tag.
In order to do this without jQuery you need to create XMLHttpRequest
object directly. The exact code depends on the browsers you are targeting, but you can find a lot of examples online. A good place to start is w3schools.com
You should have a look at: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started and on readyState == 4 the request is completed. A successful request results in a HTTP 200 message:
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
These are the guarding clauses, which check the result subsequentially. After both checks pass, you could append your result (httpRequest.responseXML) to the DOM.