-
-
Save Comandeer/96713f2e0428a58525e93478f840c0be to your computer and use it in GitHub Desktop.
fetchAndParse helper function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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