- A sandboxed iframe keeps your code apart from this site
- Every run creates a new iframe whose sandbox allows scripts, dialogs (alert, confirm, prompt), forms, pop-ups, pointer lock and downloads—but not allow-same-origin or allow-top-navigation. Your page therefore runs as its own opaque origin: it cannot read this site’s DOM, localStorage or cookies, and it cannot navigate the tab away. The previous iframe is removed on the next run, so its timers, animations and sounds stop with it.
- Why the line numbers match your code
- The console bridge is placed at the very start of line 1 (right after the DOCTYPE, if there is one) and contains no line breaks, so your code does not move down a line. The CSS and JavaScript panels are appended after the HTML, and error line numbers are mapped back to the panel they came from. In Chrome, Edge and Firefox the preview is an srcdoc document, so APIs that need a secure context, such as crypto.randomUUID, work as usual. In Safari it is loaded from a data: URL instead: in our tests WebKit, the engine behind Safari, reports every uncaught error in an srcdoc document only as “Script error.” with no line.
- How the console is forwarded
- The page’s console methods are wrapped. Arguments are turned into text inside the sandbox (objects three levels deep, at most 100 array items, circular references as [Circular]) and sent with postMessage. The tool accepts only messages from the current preview that carry this run’s random token, and shows them only as text. Console output is limited to 500 messages per second and the rest is only counted, so a console.log in an infinite loop or in every animation frame cannot drag the page down; errors are counted separately, so a flood of output never hides them.
- localStorage, sessionStorage and cookies in the sandbox
- In an opaque-origin page, reading localStorage or sessionStorage throws a SecurityError; writing document.cookie throws in Chromium and is silently dropped in Safari. Before your code runs, the tool swaps in in-memory stand-ins: getItem and setItem, the localStorage.key = value form and document.cookie all work, and the data is cleared when the preview reloads.
- Detecting a file’s encoding
- When you open an .html file, the encoding is taken from, in order: a byte order mark; UTF-8, if the bytes are valid UTF-8; a <meta charset> in the first 1024 bytes (the same range a browser pre-scans), which can name any encoding the browser knows, such as GB2312, Big5 or Shift_JIS; and otherwise the bytes themselves, choosing between GBK and UTF-16. The detection is shared with this site’s Encoding Converter.