In an iframe
This page is for showing a form on a site other than the one that serves it: how to size the frame, how the two pages talk, and what changes inside a frame.
The form is a custom element with a shadow root, so on most pages it needs no frame: the page's styles do not leak into it and its styles do not leak out. An iframe is for the cases where the form has to be a page of its own:
- The form is served by one site and shown on another.
- The host page's content policy cannot be loosened for the form's files.
- The host is a content system that accepts an embed address and nothing else.
The whole page
Two files: the host page, and the page it frames. The framed page holds the form and delivers the answers itself. It tells the host two things through post: how tall it is, and that the form was sent.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Apply</title>
<style>#intake { display: block; width: 100%; border: 0; }</style>
</head>
<body>
<h1>Apply</h1>
<p id="status">Not sent yet.</p>
<iframe id="intake" src="/embed/intake.html" title="Customer intake"></iframe>
<script type="module">
const frame = document.getElementById("intake");
window.addEventListener("message", (event) => {
// Only the framed page, and only from the origin it is served on.
if (event.source !== frame.contentWindow || event.origin !== location.origin) return;
if (event.data.type === "intake:height") {
frame.style.height = event.data.height + "px";
}
if (event.data.type === "intake:submitted") {
document.getElementById("status").textContent = "Sent. Confirmation " + event.data.reference + ".";
}
});
</script>
</body>
</html>Nothing yet.
Sizing the frame
A frame does not grow with its content. The framed page watches its own height and reports it, and the host sets the frame's height from the message. Set body { margin: 0; } in the framed page so the reported height is exact, and a validation error or a receipt that lengthens the form resizes the frame with it.
Talking across the frame
Keep the messages few and plain. In the example the answers never cross the frame: the framed page posts them to its own server and tells the host only the confirmation number.
Both sides name the other's origin:
- The framed page passes the host's origin as the second argument of
post, so the message is delivered to that site and no other.Message - The host checks
event.against the frame andsource event.against the site the form is served from, and ignores everything else.origin
In the example both pages are on one site, so both use location.. Across two sites, write each site's origin out.
What the server of the framed page sends
A page can be framed only where its server allows it. On the framed page's responses, name the hosts that may frame it:
Content-Security-Policy: frame-ancestors https://www.example.orgWithout that header a browser frames the page anywhere, which is rarely what a form wants.
What changes inside a frame
- Drafts. A draft kept in the browser belongs to the framed page's origin. When that origin differs from the host's, browsers partition or block its storage, so a draft may not survive a return visit. Keep drafts on the server instead: see Drafts.
- Passkeys. Signing a submission with a passkey from inside a cross-origin frame needs
allow="publickey-credentials-get"on the<iframe>. See Signing. - Browser assistants. The assistant tools need
allow="tools"on the frame: see Browser assistants. - Printing and PDFs. Both work from the framed page as they do on a page of its own.
- The language. The host cannot set an attribute on a form in another page. Pass the locale in the frame's address and read it in the framed page, or let the form's own switcher do it.
Next
- The headers the framed page needs for its own files: Security headers.
- Everything the framed page can set on the form: The form on your page.