Skip to content

Commit

Permalink
Add explanation that individual initialisation will throw errors
Browse files Browse the repository at this point in the history
Co-authored-by: Colin Rotherham <colin.rotherham@digital.cabinet-office.gov.uk>
  • Loading branch information
romaricpascal and colinrotherham committed Oct 2, 2023
1 parent 368ab21 commit 596c0b1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions source/importing-css-assets-and-javascript/index.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ Then import the JavaScript file before the closing `</body>` tag of your HTML pa
</body>
```

Errors from components will be logged in the browser's console.

For example, when:

* GOV.UK Frontend is not supported in the current browser
* Component templates have missing changes from our release notes
* Component JavaScript configuration does not match our documentation

#### Select and initialise an individual component

You can select and initialise a specific component by using its `data-module` attribute. For example, use `govuk-radios` to initialise the first radio component on a page:
Expand All @@ -176,6 +184,13 @@ You can select and initialise a specific component by using its `data-module` at
</script>
```

When initialised individually, components will throw errors if GOV.UK Frontend is not supported, some key markup is missing or their configuration is invalid.

It's up to you to decide what to do with these errors and either:

- let them break the flow of execution
- handle the errors with [a `try/catch` block](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch)

### Import JavaScript using a bundler

If you decide to import using a bundler, we recommend you use `import` to only import the JavaScript for components you're using in your service. For example:
Expand Down

0 comments on commit 596c0b1

Please sign in to comment.