Skip to content

Instantly share code, notes, and snippets.

@Comandeer
Created July 7, 2018 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Comandeer/96713f2e0428a58525e93478f840c0be to your computer and use it in GitHub Desktop.
Save Comandeer/96713f2e0428a58525e93478f840c0be to your computer and use it in GitHub Desktop.
fetchAndParse helper function
window.loadComponent = ( function() {
function fetchAndParse( URL ) {
return fetch( URL ).then( ( response ) => {
return response.text();
} ).then( ( html ) => {
const parser = new DOMParser();
const document = parser.parseFromString( html, 'text/html' );
const head = document.head;
const template = head.querySelector( 'template' );
const style = head.querySelector( 'style' );
const script = head.querySelector( 'script' );
return {
template,
style,
script
};
} );
}
function loadComponent( URL ) {
return fetchAndParse( URL );
}
return loadComponent;
}() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment