How live HTML, CSS and JavaScript playgrounds work
A code playground is a page with editors for HTML, CSS and JavaScript and a preview that updates as you type. Under the hood it builds a small web page from your text and runs it in an isolated frame.
The basic loop
- You type into three editors.
- After a short pause, the playground joins the three texts into one HTML document.
- It loads that document into an iframe, a frame that holds a separate page.
- The browser renders the frame like any other page, and your script runs inside it.
Step 2 is just string building. The CSS goes into a <style> element in the head, the HTML into the body, and the JavaScript into a <script> element at the end of the body so the elements already exist when it runs:
function buildDoc(html, css, js) {
return '<!doctype html><html><head><meta charset="utf-8">' +
'<style>' + css + '</style></head><body>' + html +
'<script>' + js + '<\/script></body></html>';
}
preview.srcdoc = buildDoc(htmlBox.value, cssBox.value, jsBox.value);
The backslash in <\/script> matters when this code itself sits inside an HTML page: without it, the browser would end the outer script early.
Why the sandbox is essential
The code in a playground may come from anyone, for example a shared link. If it ran in the same page as the editor, it could read the editor’s cookies, change its interface or send requests as the logged-in user. The sandbox attribute on the iframe removes those powers:
<iframe id="preview" sandbox="allow-scripts" title="Preview"></iframe>
| Sandbox setting | Effect |
|---|---|
sandbox with no value | No scripts, no forms, no pop-ups; a unique origin |
allow-scripts | JavaScript runs, but still in a unique origin |
allow-same-origin | The frame keeps the parent’s origin |
allow-forms, allow-modals, allow-popups | Each re-enables one feature |
Never combine allow-scripts with allow-same-origin for untrusted code in a frame from your own origin. Together they let the script reach into the parent page and remove its own sandbox. Serious playgrounds go further and serve previews from a separate domain, so even a mistake cannot touch the main site’s data.
Updating without lag
Rebuilding the frame on every keystroke wastes work and restarts animations constantly. Playgrounds debounce the update: each keystroke resets a short timer, and the rebuild runs only when the timer finishes.
let t;
editor.addEventListener('input', () => {
clearTimeout(t);
t = setTimeout(render, 300);
});
Example. You type 12 characters, each about 100 ms apart, then stop. With a 300 ms debounce, no rebuild runs while you type because each gap is shorter than the timer. One rebuild runs 300 ms after the last key, instead of twelve rebuilds.
Showing console output
A sandboxed frame’s console.log goes to the browser’s own DevTools, not to the playground. To show logs in the page, the playground injects a small script before yours that wraps console.log and sends each message to the parent with parent.postMessage(). The parent listens for the message event, checks that it came from the preview frame, and prints the text. Listening for the frame’s error event in the same way surfaces syntax errors with line numbers.
Features that need care
- Infinite loops. A
while (true)freezes the frame and can freeze the tab. Some playgrounds rewrite loops to add a time check; a simple one tells users to reload. - External libraries. Loading a library from a CDN means a network request. Some playgrounds pin exact versions so a shared example keeps working later.
- Saving work. Storing code in the address after a
#keeps it in the link without a server, but long code makes very long links.
Practising without a playground
You do not need an online tool. Create a file named test.html, write HTML with a <style> and a <script> block, and open it in your browser. Reload after each save, and use the DevTools Console for errors. It is the same loop, done by hand, and your code never leaves your computer.
Questions
What is the difference between srcdoc and a blob URL?
Both load a document built in the page. srcdoc takes the HTML as an attribute value; a blob URL is created from a file-like object with URL.createObjectURL(). Either works inside a sandboxed frame.
Can a playground run server-side languages?
Not by itself. Running Python or PHP needs a server, or an interpreter compiled to WebAssembly that runs in the browser.