Iframe Integration
Intuition
Section titled “Intuition”Embedding interactive tools in docs: Iframes are like picture-in-picture on your TV — they let you embed external tools (like Compiler Explorer) directly in your documentation, so readers can experiment without leaving the page.
Why it matters: Interactive examples dramatically improve learning — readers can modify code, see results instantly, and build intuition through experimentation, rather than just reading about concepts.
The key insight: Iframes sandbox external content, so a broken embed cannot crash your page — but they can be slow to load and may not work on all devices, so always provide a fallback link.
Godbolt
Section titled “Godbolt”- Theming is not embedded with iframe
- Font size is embedded with iframe
Dartpad
Section titled “Dartpad”- Gemini cannot be turned off
- Place gist after
?id=and before&split
Security Considerations
Section titled “Security Considerations”The sandbox Attribute
Section titled “The sandbox Attribute”The sandbox attribute restricts what an iframe is allowed to do. Always use it — omitting it Grants the embedded page the same privileges as the parent page.
| Token | Effect |
|---|---|
| (empty) | Most restrictive: no scripts, no forms, no same-origin, no popups |
allow-scripts | Allows JavaScript execution |
allow-same-origin | Allows the iframe to be treated as same-origin (needed for API access) |
allow-popups | Allows window.open / target="_blank" |
allow-forms | Allows form submission |
allow-downloads | Allows downloading files |
allow-top-navigation | Allows the iframe to navigate the top-level page (avoid this) |
Content Security Policy (CSP)
Section titled “Content Security Policy (CSP)”If your site sets a CSP via HTTP headers or <meta> tags, iframes add complexity:
frame-src(or the olderchild-src) controls which URLs can be embedded.frame-ancestorson the embedded site controls who can embed it. Many services setframe-ancestors "self'to prevent embedding, which is why not every site works inside an iframe.
Example CSP header that allows Godbolt and Dartpad:
Content-Security-Policy: frame-src https://godbolt.org https://dartpad.dev;HTTPS Only
Section titled “HTTPS Only”Always use https:// for iframe src values. Browsers will block mixed content (loading HTTP Iframes from an HTTPS parent) without warning the user — the iframe will not render.
Cross-Origin Communication with postMessage
Section titled “Cross-Origin Communication with postMessage”When the parent page and iframe are on different origins, direct DOM access is blocked. Use window.postMessage to send data between them.
Parent to iframe
Section titled “Parent to iframe”const iframe = document.querySelector('iframe');const iframeWindow = iframe.contentWindow;
iframeWindow.postMessage({ type: "godbolt-set-language'', language: "c++' }, 'https://godbolt.org');Iframe to parent
Section titled “Iframe to parent”Inside the embedded page:
window.parent.postMessage( { type: "compilation-result'', asm: "...' }, 'https://your-docusaurus-site.com',);Receiving messages
Section titled “Receiving messages”window.addEventListener('message', (event) => { if (event.origin !== 'https://godbolt.org') return; if (event.data.type === 'compilation-result') { console.log('Assembly output: ", event.data.asm); }});Key rules:
- Always check
event.origin. Never trust a message without verifying where it came from. - Use a structured
dataobject with atypefield so the listener can dispatch appropriately. - Specify
targetOriginexplicitly inpostMessage— never use''*"if you know the target.
Responsive Iframes
Section titled “Responsive Iframes”Fixed width and height attributes work but break on narrow screens. Common approaches:
Aspect Ratio Method
Section titled “Aspect Ratio Method”Use the CSS aspect-ratio property to maintain proportions:
.iframe-wrapper { width: 100%; aspect-ratio: 16 / 9; overflow: hidden;}
.iframe-wrapper iframe { width: 100%; height: 100%; border: none;}Scrollable Container
Section titled “Scrollable Container”When the embedded content is taller than the viewport:
.iframe-scroll-container { width: 100%; max-height: 600px; overflow-y: auto; border: 1px solid var(--ifm-color-emphasis-300); border-radius: 6px;}
.iframe-scroll-container iframe { width: 100%; min-height: 100%; border: none; display: block;}Docusaurus-Specific Wrapper
Section titled “Docusaurus-Specific Wrapper”This site uses a godbolt-container CSS class (though currently not defined in custom.css — add it If needed):
.godbolt-container { width: 100%; margin: 1.5rem 0; border: 1px solid var(--ifm-color-emphasis-300); border-radius: 6px; overflow: hidden;}This wraps the iframe with consistent spacing and a subtle border.
Lazy Loading
Section titled “Lazy Loading”The loading="lazy" attribute defers iframe loading until the element is near the viewport. This is Critical for pages with multiple or heavy embeds.
<iframe src="https://godbolt.org/e#..." loading="lazy" title="Compiler Explorer"></iframe>Browser support is universal for loading="lazy" on iframes. There is no need for a JavaScript-based intersection observer unless you need precise control over when the iframe Initializes.
Lazy Loading with srcdoc Placeholder
Section titled “Lazy Loading with srcdoc Placeholder”For a polished experience, show a placeholder and swap the real src on interaction:
function LazyIframe({ src, title }) { const [loaded, setLoaded] = React.useState(false);
if (!loaded) { return ( <button onClick={() => setLoaded(true)} style={{ padding: "2rem'', width: "100%', cursor: "pointer'' }} > Click to load {title} </button> ); }
return ( <iframe src={src} title={title} width="100%" height="600" sandbox="allow-scripts allow-same-origin" /> );}This approach saves bandwidth and avoids executing third-party scripts until the user explicitly Opts in.
Common Pitfalls
Section titled “Common Pitfalls”X-Frame-Options and CSP Blocking
Section titled “X-Frame-Options and CSP Blocking”If an iframe is blank or shows a browser error, the target site likely sets X-Frame-Options: DENY Or SAMEORIGINOr uses frame-ancestors in CSP. There is no workaround — the site owner must Allow embedding.
Cookie Partitioning (Third-Party)
Section titled “Cookie Partitioning (Third-Party)”Modern browsers (Chrome, Firefox, Safari) partition third-party cookies in iframes. If the embedded Service relies on cookies for authentication or preferences, the user may need to interact with the Service directly first.
Focus Trapping
Section titled “Focus Trapping”When an iframe gains focus (e.g., user clicks inside it), keyboard events are captured by the Iframe. The parent page loses keyboard navigation. This is by design but can confuse users who Expect Tab / Escape to work for the parent site. Consider adding a visual hint near interactive Iframes.
Print Issues
Section titled “Print Issues”Iframes do not print well. The browser may show a blank rectangle or only the visible portion. If Printable content is needed, extract the data and render it in the parent page instead of embedding.
Mobile Performance
Section titled “Mobile Performance”Each iframe creates a separate browsing context with its own document, scripts, and style sheets. On Mobile devices with limited memory, multiple iframes can cause significant slowdowns. Limit the Number of iframes per page and prefer lazy loading.
Docusaurus-Specific Patterns
Section titled “Docusaurus-Specific Patterns”Embedding in MDX
Section titled “Embedding in MDX”In Docusaurus MDX files, iframes are written as JSX:
<iframe width="100%" height="500" src="https://example.com/embed" title="Descriptive title" sandbox="allow-scripts" loading="lazy"/>The title attribute is required for accessibility. Docusaurus build will warn if it is missing.
Wrapping in a Component
Section titled “Wrapping in a Component”For repeated embed patterns, create a React component in src/components/:
function CodeEmbed({ src, height = 500, title = "Code embed' }) { return ( <div className="godbolt-container"> <iframe width="100%" height={height} src={src} title={title} sandbox="allow-scripts allow-same-origin" loading="lazy" /> </div> );}Then use it in any MDX file:
import CodeEmbed from '@components/CodeEmbed';
<CodeEmbed src="https://godbolt.org/e#..." title="My snippet" height="600" />Godbolt URL Construction
Section titled “Godbolt URL Construction”Godbolt share URLs are long encoded strings. To construct one manually:
- Open godbolt.org and set up the compiler, flags, and source code.
- Click “Share” and copy the URL.
- Everything after
#is the state hash. URL-encode it if embedding in a query parameter.
The base embed URL pattern is:
https://godbolt.org/e#<encoded-state>Godbolt supports additional embed parameters:
| Parameter | Description |
|---|---|
#z:... | Compressed source and compiler state |
&hideEditor | Hides the source editor panel |
&hideOutput | Hides the compiler output panel |
Dartpad URL Construction
Section titled “Dartpad URL Construction”Dartpad embed URLs follow this pattern:
https://dartpad.dev/embed-inline.html?id=<gist-id>&split=<ratio>&theme=<dark|light>| Parameter | Description |
|---|---|
id | GitHub Gist ID containing the Dart source |
split | Editor/preview split ratio (0–100) |
theme | dark or light — must match the parent site |
Note that Dartpad’s theme does not inherit from the parent page. It must be set explicitly in the URL. If the site supports dark mode toggling, the Dartpad theme will not switch dynamically without JavaScript intervention.
Summary
Section titled “Summary”This topic covers the mathematical techniques and concepts related to iframe integration, including key theorems, methods, and problem-solving approaches.
Key concepts include:
- fundamental definitions and theorems
- algebraic and graphical methods
- proof and logical reasoning
- problem-solving strategies
- applications and modelling
Regular practice with a variety of question types is essential to build fluency and confidence in applying these mathematical techniques.
Worked Examples
Section titled “Worked Examples”Worked examples demonstrating the application of key concepts are covered in the detailed sub-pages linked above.