skip to content
dz

Back to the article · Zurück zum Artikel (Deutsch)

Example 02: A fragment and the excerpt the article shows

Showing only an excerpt

A button and a script. The article shows only the handler region of this fragment, the page runs all of it and lists the complete source at the bottom.

How to try it

  1. Click "Count" a few times. The "Console output" box and the console of the developer tools (F12, Cmd+Opt+I on a Mac) show one line per click.
  2. Compare the source at the bottom with the article: the lines with #region are not displayed there.
Console output mirrored from console.log() and errors on this page, the same text as in the developer tools
Expected output what the lines above should say
clicks: 1
clicks: 2
clicks: 3

After three clicks.

Live example

Source

<button id="count" type="button">Count</button>

<script>
	// Everything outside the region below runs on this page, but the article
	// shows only the region.
	let clicks = 0;

	document.getElementById('count').addEventListener('click', () => {
		clicks += 1;
		console.log('clicks:', clicks);
	});
</script>