Cross-site HTML include via <script>
tags in the manner of JSONP.
Basic features:
- Bypass CORS, usable with
file://
protocol - Retains the synchronous programming model
- Preserves order-of-evaluation for nested includes
- Parser-blocking for FOUC suppression
Advanced features:
data-dir
attribute allows specifying include base directorylink()
function to remap relative paths inside includesdata
andtemplate
variables for passing data to include files<include-once>
tag allowing smarter resource management- Detect and block infinite include loops (bypass-able)
The following just works, even from your local drive:
<!DOCTYPE html>
<html>
<head>
<script src="js/x-include.js" data-dir="../includes"></script>
<script>include('head.js', {title: "Hello World"})</script>
</head>
<body>
<script>include('header.js')</script>
<main>
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pellentesque neque vitae varius facilisis.</p>
</main>
<script>include('footer.js')</script>
</body>
</html>
// head.js
include.html(x=>`
<title>${x.data.title}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/svg+xml" href="${x.link("../img/favicon.svg")}">
<link rel="stylesheet" media="screen" href="${x.link("../css/style.css")}">
`)