Include another HTML file in a HTML file
Inject an external HTML into the DOM using the JavaScript fetch
API or just load it in with an iframe:
Or load content separately in an iframe:
Use JavaScript for seamless integration, iframes for compartmentalization.
HTML presto! The jQuery .load() method
jQuery .load()
allows to include HTML files swiftly. First, add jQuery to your main file 'a.html' in <head>
:
Next, mark where you'd like b.html
to show up in a.html
:
Lastly, make it happen with jQuery .load()
:
Writing on the Wall with document.write
JavaScript's document.write
appends HTML directly into the DOM during page load. It's simple and classic like a vinyl record.
Remember: handle with care! HTML special characters need escaping.
A Juicer with .js flavour
Converting HTML files b.html
into .js
files works like a charm too. JavaScript template literals allow for multiline inclusion, like a perfectly folded burrito.
HTML5 imports: The Interface from the Future
HTML imports, part of the Web Components kick, allow imports straight into head
:
Sadly, it's on the chopping block due to limited browser support.
The Harmony of HTML file organization
Create views/
directory for housing CHUNKS of reusable HTML. Like well-trained hummingbirds fetching nectar, these can be drafted easily for display.
Beating the Clutter: Handling multiple inclusion
Use custom data-include
attributes for including components dynamically and easily. Commands at your fingertip!
Lightning-fast Server-Side Includes (SSI)
Having a web host that supports Server-Side Includes(SSI) lets you leverage its power for snappier page loads.
Your site host's documentation is the potion book for SSI magic!
Element tooltips with JavaScript's Tooltip library
Employ a JavaScript library called Tooltip.js to manage dynamic tooltips segregating them in HTML elements for easy mapping.
Third-party Magic: csi.js
Third-party solutions like csi.js
by LexmarkWeb aid in embedding HTML vigorously. Comprehensive guidelines available too!
Was this article helpful?