Determine whether the browser supports web sockets

Time:2020-6-17

At present, web sockets are not well supported from browser to terminal. Only ie 10 +, FF 34 +, chrome 31 +, Safari 7.1 +, Android browser 4.4 + are supported.

Determine whether the browser supports web sockets

Click here for details

By what means?

The simple way is to directly determine whether a websocket object is included in the global object

javascript$(document).ready(function() {
  if( typeof(WebSocket) != "function" ) {
    $('body').html("<h1>Error</h1><p>Your browser does not support HTML5 Web Sockets. Try Google Chrome instead.</p>");
  }
})

But this method has limitations.

In Android, even if the browser does not support websocket, it still has this property.

So the more rigorous way is:

javascriptif (typeof WebSocket != 'undefined') {
    /*supported*/ 
}

//or
if (!!window.WebSocket && window.WebSocket.prototype.send) {
    /*supported*/
}

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]