Events
onlanguagechange
The browser's preferred language settings change. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: Event. For the described native event: bubbles: no; cancelable: no. Read navigator.languages again; this does not translate existing page content automatically. Example: Change browser language preferences if supported. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.
Formal Syntax & Structure
html syntaxhtml
<body onlanguagechange="handleEvent(event)"> window.addEventListener("languagechange", event => { /* handle event */ });Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| onlanguagechange | JavaScript handler body | Optional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute. |
| event | Event | Event object delivered to the addEventListener callback; event.type is "languagechange". Inspect target, currentTarget, bubbles and cancelable as appropriate. |
Working Example
Example Snippethtml
<p>Window event monitor</p><p role="status" id="log">Waiting for languagechange</p><script> const target = window; const log = document.getElementById("log"); target.addEventListener("languagechange", event => { log.textContent = String(navigator.languages.join(", ")); });</script>