此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

CloseEvent:reason 属性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

备注: 此特性在 Web Worker 中可用。

CloseEvent 接口的只读属性 reason 返回服务器为关闭连接而提供的 WebSocket 连接关闭原因;即一个简洁的人类可读的关闭原因说明。

字符串。

示例

以下示例将 reason 的值打印到控制台。

js
WebSocket.onclose = (event) => {
  console.log(event.reason);
};

规范

Specification
WebSockets
# ref-for-dom-closeevent-reason②

浏览器兼容性

参见