Skip to content

Commit

Permalink
Merge pull request #1868 from alphagov/update-character-count-back
Browse files Browse the repository at this point in the history
Preserve the state of character count when navigating 'back'
  • Loading branch information
Vanita Barrett committed Jul 16, 2020
2 parents 75c3ddc + 5a344b3 commit ce8e223
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ We’ve made fixes to GOV.UK Frontend in the following pull requests:
- [#1855: Hint component can render block-level elements as valid HTML](https://github.com/alphagov/govuk-frontend/pull/1855)
- [#1861: Fix the display of checkboxes when border-box box sizing is applied globally](https://github.com/alphagov/govuk-frontend/pull/1861)
- [#1862: Fix display of warning text icon when border-box box sizing is applied globally #1862](https://github.com/alphagov/govuk-frontend/pull/1862)
- [#1848: Preserve the state of the character count when navigating 'back' in the browser](https://github.com/alphagov/govuk-frontend/pull/1848)

## 3.7.0 (Feature release)

Expand Down
14 changes: 14 additions & 0 deletions src/govuk/components/character-count/character-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ CharacterCount.prototype.init = function () {
// Remove hard limit if set
$module.removeAttribute('maxlength')

// When the page is restored after navigating 'back' in some browsers the
// state of the character count is not restored until *after* the DOMContentLoaded
// event is fired, so we need to sync after the pageshow event in browsers
// that support it.
if ('onpageshow' in window) {
window.addEventListener('pageshow', this.sync.bind(this))
} else {
window.addEventListener('DOMContentLoaded', this.sync.bind(this))
}

this.sync()
}

CharacterCount.prototype.sync = function () {
this.bindChangeEvents()
this.updateCountMessage()
}
Expand Down

0 comments on commit ce8e223

Please sign in to comment.