javascript - Load specific element from another page with vanilla js - Stack Overflow

PHOTO EMBED

Tue Jul 12 2022 17:54:11 GMT+0000 (Coordinated Universal Time)

Saved by @richard #javascript

function getPage(url, from, to) {
    var cached=sessionStorage[url];
    if(!from){from="body";} // default to grabbing body tag
    if(to && to.split){to=document.querySelector(to);} // a string TO turns into an element
    if(!to){to=document.querySelector(from);} // default re-using the source elm as the target elm
    if(cached){return to.innerHTML=cached;} // cache responses for instant re-use re-use

    var XHRt = new XMLHttpRequest; // new ajax
    XHRt.responseType='document';  // ajax2 context and onload() event
    XHRt.onload= function() { sessionStorage[url]=to.innerHTML= XHRt.response.querySelector(from).innerHTML;};
    XHRt.open("GET", url, true);
    XHRt.send();
    return XHRt;
}
content_copyCOPY

https://stackoverflow.com/questions/21435877/load-specific-element-from-another-page-with-vanilla-js