Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a native javascript equivalent to jquery .load()

What would be the native javascript equivalent to the jquery below?

$('#anyDiv').load("anyPage.htm");
like image 484
MarkL Avatar asked Dec 15 '22 12:12

MarkL


2 Answers

Yes, there is:

function load(target, url) {
  var r = new XMLHttpRequest();
  r.open("GET", url, true);
  r.onreadystatechange = function () {
    if (r.readyState != 4 || r.status != 200) return;
    target.innerHTML = r.responseText;
  };
  r.send();
}

load(document.getElementById('anyDiv'), 'anyPage.htm');
like image 81
Rob M. Avatar answered Dec 31 '22 06:12

Rob M.


You can try this,

window.onload = () => {
    fetch('/path/to/page.html')
    .then(data => {
      return data.text()
    })
    .then( data => {
      document.getElementById("parentContainer").innerHTML = data;
    })
}
like image 22
Nikhil Singh Avatar answered Dec 31 '22 07:12

Nikhil Singh