Consider this Svelte component that uses progressive enhancement to show a message only if JavaScript is enabled.
<noscript>Please enable JavaScript to see the message.</noscript>
<script>
let showMessage = false;
onMount(() => {
showMessage = true;
});
</script>
{#if showMessage}
<p>JavaScript is enabled!</p>
{/if}
What will the user see if JavaScript is disabled?
<noscript>Please enable JavaScript to see the message.</noscript> <script> import { onMount } from 'svelte'; let showMessage = false; onMount(() => { showMessage = true; }); </script> {#if showMessage} <p>JavaScript is enabled!</p> {/if}
Think about what the <noscript> tag does when JavaScript is disabled.
The <noscript> tag content is only shown if JavaScript is disabled. Since JavaScript is off, the script block and the conditional block do not run, so only the <noscript> message appears.
count after this Svelte component runs?This Svelte component increments a counter only if JavaScript is enabled. What is the value of count after the component mounts?
<noscript></noscript>
<script>
import { onMount } from 'svelte';
let count = 0;
onMount(() => {
count += 1;
});
</script>
<p>Count: {count}</p>
<noscript></noscript> <script> import { onMount } from 'svelte'; let count = 0; onMount(() => { count += 1; }); </script> <p>Count: {count}</p>
Remember when onMount runs in Svelte.
The onMount function runs only when JavaScript is enabled and the component is mounted. It increments count by 1, so the final value is 1.
You want a button that submits a form normally if JavaScript is disabled, but uses Svelte's event handler when JavaScript is enabled. Which code snippet achieves this?
Think about the default behavior of a submit button and how to preserve it when JavaScript is disabled.
Using type="submit" ensures the button submits the form normally when JavaScript is off. Adding on:click={handleClick} enhances it with JavaScript behavior without preventing default submission.
Given this code, the fallback message inside <noscript> does not appear when JavaScript is disabled. Why?
<script>
let jsEnabled = false;
onMount(() => {
jsEnabled = true;
});
</script>
{#if !jsEnabled}
<noscript>Please enable JavaScript</noscript>
{/if}
<script> import { onMount } from 'svelte'; let jsEnabled = false; onMount(() => { jsEnabled = true; }); </script> {#if !jsEnabled} <noscript>Please enable JavaScript</noscript> {/if}
Consider how <noscript> works and when Svelte renders blocks.
The <noscript> tag only works if it is directly in the HTML. Placing it inside a Svelte conditional means it is controlled by JavaScript rendering, so it won't appear if JS is disabled.
Choose the statement that most accurately explains how progressive enhancement works in Svelte.
Think about how Svelte can deliver content that works with or without JavaScript.
Progressive enhancement in Svelte means starting with simple HTML that works without JavaScript, then adding interactivity and reactivity when JavaScript is available.