Open An Html File Inside Another Html(i.e. Template)
I have a banner html with a bunch of buttons(i.e. home, about,..etc.) that I'd like to set as a template. On my unique pages(say the home page), I'd like to 'import' this template
Solution 1:
You can use HTML Imports to import an external HTML file with a <template>
element where you add the elements you want to .
In the main file:
<head>
<link href="template.html"id="myTemplate">
</head>
<body>
<div id="container"></div>
</body>
In the template.html
file:
<template><span>Hello World!</span></template><script>//get the ed HTML documentvar edDoc = document.querySelector( '#myTemplate' ).
//get the content of the template elementvar content = edDoc.querySelector( 'template' ).content//add a compy to the main pagedocument.querySelector( '#container' ).appendChild( content.cloneNode( true ) )
</script>
In the example above, the conent of the <template>
element (the <span>
text "Hello World!"
) will be put in the <div id=container>
element in the main page.
Update 2019
HTML Imports won't be supported natively after Chrome 73. You should then use the other solutions listed above (the polyfill, an alternate module loader, JS , or a direct download with fetch
).
Post a Comment for "Open An Html File Inside Another Html(i.e. Template)"