I simply would like to replace the <main>
-Element and it's content of firstPage.html
...
// ... header of my 1st document ...
<body>
<main>
The content of the 1st document...
</main>
</body>
// ... footer of my 1st document ...
... with the <main>
-Element and it's content of secondPage.html
...
secondPage.html
// ... header of my 2nd document ...
<body>
<main>
The content of the 2nd document...
</main>
</body>
// ... footer of my 2nd document ...
... using AJAX call by plain JavaScript. How could I do that?
I am able to several methods to load 2nd document completely into the 1st one but I can't find any solution in JavaScript to load only specific HTML-element from another URL into DOM of 1st document. :(
UPDATE/EDIT:
This is my status quo of my ajax function:
var myLoad = ( type, url, data, success ) => {
var d = data ? ( typeof data == 'string' ) ? data : Object.keys(data).map(k=>( encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) )).join('&') : '' ;
var u = (d && type==='GET') ? url+'?'+d : url ;
var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
x.open(type,u);
x.onreadystatechange = () => {
if( x.readyState > 3 && x.status == 200 ){
success(x.responseText)
}
};
x.setRequestHeader('X-Requested-With','XMLHttpRequest');
if( type === 'POST' ){
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
x.send(d);
} else {
x.send();
}
return x;
};
myLoad( 'GET', 'secondPage.html', '', r => {
// of course this loads the whole document (secondPage.html) into <main>
// but how to do otherwise ???
document.querySelector('main').innerHTML = r;
};