devroom.io/drafts/2010-04-05-detect-browser-web-sockets-support.md

28 lines
996 B
Markdown
Raw Normal View History

2013-03-22 22:53:57 +00:00
---
title: "Detect browser Web Sockets support"
kind: article
slug: detect-browser-web-sockets-support
created_at: 2010-04-05
tags:
- html5
- websockets
- javascript
- jquery
---
HTML5 Web Sockets are awesome! I've been toying around with it for a bit today and noticed that not every browser supports native HTML5 Web Sockets yet.
Google Chrome 5 has native support for web sockets, FireFox 3.6 does not. This poses a problem if you're building something awesome that does require web sockets.
~
Luckily, it's easy to detect web sockets support through JavaScript. All you really need to do is check if `WebSocket` is defined or not.
Here's a simple example, note that I'm using jQuery here.
:::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>");
}
});
Maybe there are better ways, but _it works for me_.