Docs / Pages and packages
Web pages
A Web Snippet is a real web page that runs beside the editor, with its console a tab away.
On this page
Start one from the Web Page Starter, or from React, Vue, Svelte and the other browser Starters (Starters). The page runs in a real browser, in a process of its own, with a web page’s access and no more.
Edit the page in Parts
A Web Snippet is one HTML document. The strip above the editor shows it in three Parts, one at a time:
- HTML: the markup inside
<body>. - CSS: the styles inside
<style>. - JS (or TS in TypeScript): the code inside the last
<script>. It is the only Part that is checked and run.
Markup, style and script
Click a tab to edit that Part. Each Part has its own undo, so undoing in the CSS never takes back what you typed in the JS. A line number in the console is a line of the script Part, unless it names another. Click it, and JSpark opens that Part and goes to the line.
Code in the HTML Part is counted there. A console row printed by an inline <script> or an on… handler, such as onclick="hello()", ends with its line in the HTML Part: hold the pointer on it to read Go to HTML line 4. An error’s functions read HTML line 4 there. Click either to open the HTML Part at that line.
A React, Preact or Solid Snippet shows JSX and CSS only: its markup is the element the library renders into. A Vue or Svelte Snippet has no strip. It is one component file, edited whole, as in a Vue or Svelte project.
Run the page
Press ⌘↵ or Ctrl+Enter to run the page. It appears beside the code, and what it prints appears in its Console tab. Before the first Run, the pane says Run it to see the page.
Components and hooks
0
−+- With Run as you type on, the page is built again as you type. When the build changes, the page reloads and starts over, but stays scrolled where you were. An edit that changes nothing, such as a comment, leaves the page as it is.
- An edit that changes only the CSS, Tailwind classes included, is put into the page without a reload. A count you clicked or a field you filled keeps its value.
- If an edit stops the code from building, the page keeps the last version that worked. A bar across its top says Showing the last working version — fix the error on line 4, and the line is a link.
- If the page draws nothing and logs an error, the pane says Nothing rendered, and the page logged an error. It links to the line of the first error the page threw, or says see the console when only
console.errorwas called. - The page keeps running, and keeps its state, while another tab is on screen.
- A page that never finishes can’t hang JSpark: it runs in its own process.
- The page can call APIs with
fetch, as any web page can. - The Timeline replays Node Snippets only. A page has no Timeline.
Style the page
The CSS Part is the page’s stylesheet, in its <head>. JSpark adds only a margin reset and a default font before it, so your rules win. A <style> you write inside the HTML Part stays part of the markup.
With @import "tailwindcss"; in the CSS Part, JSpark builds the stylesheet with Tailwind before the page loads. Every Starter with a CSS Part begins with that line. See Tailwind CSS.
Load code from the web
A page may load code from the web, as any page can. Each of these works:
- An import from a URL in the script, such as
import { format } from "https://esm.sh/date-fns". - A
<script src="https://…">, a stylesheet or a font from anhttps:address, in the HTML Part. - An import map in the HTML Part, so the script can import by name.
- Inline handlers you wrote in the HTML, such as
onclick="hello()", andeval.
Code from the web needs a connection. A package imported by name, such as react in the React Starter, comes from the Packages window instead. When your page’s import map names a package, your mapping wins.
One thing is refused: a script added after the page started, for example through innerHTML. The console says when something was refused.
The page and its console
The output side has two tabs, Page and Console. Each one gets the whole side. Click a tab to switch.
- Switching doesn’t reload the page. It keeps running while you read the console.
- While the page is on screen, a red dot on the Console tab means the page logged an error you haven’t seen yet.
- The choice is the same for every tab, and JSpark starts on the page.
- To give the page the whole window, choose Output only in the Layout menu. See Layout.
Open the page in your browser JSpark+
In JSpark+, Open in Browser opens the page in your default browser: for its own DevTools, your extensions, or a check in Safari or Firefox. The button is at the right end of the strip with the Page and Console tabs. It is ready once the page has run.
Markup, style and script
- Each time an edit changes the page, the browser’s copy reloads, so it starts from a fresh state. An edit that changes nothing on the page, such as a comment, leaves it alone.
- Its console is the browser’s DevTools, not JSpark’s console.
- When an edit stops the build, the browser keeps the last working version, as the page in JSpark does.
- A page with a server Part works too: its
fetchcalls reach its server. See A page with a server.
A small server inside JSpark serves the page, one for each tab, started the first time you use it. Only this computer can reach it, and it answers only to the page JSpark opened: another website, another app, or another Snippet’s page can’t read it or reach its server. Nothing leaves your computer.
When you close the tab in JSpark, when JSpark+ ends, or when JSpark quits, a note on the browser’s page says it no longer updates. After JSpark has quit, close that browser tab rather than reloading it: another program could answer at its address.
In JSpark, the button is marked +, and pressing it says what JSpark+ adds.
When a page freezes
When a page stops answering for more than a second, JSpark lays The page froze over it. Only the page is stuck. The editor and the rest of JSpark keep working.
Markup, style and script
The page froze
Only the page is stuck. Hiding won’t stop it.
Stop pageTo start the last version that worked again on a fresh page, without changing the code, press Refresh. To end the page, follow these steps:
- Press Stop page. The page ends, and the footer says Stopped.
- Fix the code that keeps the page busy, such as a loop that never ends.
- Press ⌘↵ or Ctrl+Enter to run it again.
If the page starts answering again by itself, the message goes away.
If the page’s process ends, because it ran out of memory or was ended outside JSpark, the pane says The page crashed. Press Refresh, or run it again, to start a fresh page.
JSpark also stops a page by itself when it is still loading after the time limit, or when it uses more memory than the memory limit. A page that has loaded and waits for a click is never stopped for time. See Time and memory limits.
The pictures show JSpark on a Mac. On Windows, the window buttons are at the top right, the menus are in theMenu button at the left end of the tabs, and shortcuts use Ctrl where a Mac uses ⌘. The rest works the same way.