Fetch API vs XHR
Fetching DOM from page
by Comandeer
JavaScript
// Fetch API
fetch( 'https://www.comandeer.pl' ).then( ( response ) => {
return response.text();
} ).then( ( html ) => {
const parser = new DOMParser();
const dom = parser.parseFromString( html, 'text/html' );
console.log( 'fetch', dom );
} );
// XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.responseType = 'document';
xhr.addEventListener( 'load', ( evt ) => {
console.log( 'xhr', evt.target.response );
} );
xhr.open( 'GET', 'https://www.comandeer.pl' );
xhr.send();