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 8e522c3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 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,12 @@ You can select and initialise a specific component by using its `data-module` at
</script>
```

When initialised individually, errors are thrown rather than logged.
This will stop the execution of the script and you'll need to either:

* fix what causes the error
* use a [`try/catch` block](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) to change the behaviour of your code in reaction to the error

### 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 8e522c3

Please sign in to comment.