Check for HTML5 features with JavaScript

There is no magic behind checking a web-browsers support for a specific HTML5 feature. If you want to check if a browser supports the latest HTML5 WebSocket API then you just have to take care if there is an object called „WebSocket“ available via the DOM:

<script>
  if('WebSocket' in window){
    alert('Hurray! Your browser supports HTML5 WebSockets.')
  }else{
    alert('Sorry, but you are out of date!');
  }
</script>

Nevertheless, there are still people who want to include a fancy library for such an inspection. You can do the same with Modernizr:

<script src="http://www.modernizr.com/downloads/modernizr-latest.js"></script>
<script>
  if(Modernizr.websockets){
    alert('Hurray! Your browser supports HTML5 WebSockets.')
  }else{
    alert('Sorry, but you are out of date!');
  }
</script>

How to check for NaN with JavaScript?

If you explicitly convert a JavaScript variable to a number (e.g. by using the parseInt function), then you have to check if the value of the converted number is really a number. In case that it is not, it will present you NaN (Not a Number). You can check for NaN with the isFinite function.

Example:

1
2
3
4
5
6
// Check for hexadecimal value
var convertedNumber = parseInt('G', 16);
if(isFinite(convertedNumber) == false)
  alert('The value of the converted number is NaN!');
else
  alert('The number is: '+convertedNumber);