The close handler is executed when a connection with a websocket is closed.

Bubbles No
Cancelable No
Interface Event
Event handler property onclose


You might want to know when the connection has been closed so that you can update the UI or, perhaps, save data about the closed connection. Given that you have a variable called exampleSocket that refers to an opened WebSocket, this handler would handle the situation where the socket has been closed.

exampleSocket.addEventListener('close', (event) => {
  console.log("The connection has been closed successfully.");

You can perform the same actions using the event handler property, like this:

exampleSocket.onclose = function (event) {
  console.log("The connection has been closed successfully.");


Specification Status
HTML Living Standard
The definition of 'WebSocket' in that specification.
Living Standard

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
closeChrome Full support YesEdge Full support 12Firefox Full support 8
Full support 8
No support 4 — 8
Notes Parameters not supported, see bug 674716.
IE ? Opera Full support YesSafari Full support YesWebView Android ? Chrome Android Full support YesEdge Mobile ? Firefox Android Full support YesOpera Android ? Safari iOS Full support YesSamsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

See also

Document Tags and Contributors

Last updated by: wbamberg,