HTML5 SVG Support check using JavaScript – the simple way

My last post talked about how you can detect whether your browser supports HTML5 Canvas or not using simple JavaScript technique. Based on the same principles, this post illustrates how you can detect if your browser supports SVG elements. You may note SVG is Scalable Vector Graphics and HTML5 has native support for SVG tags. The difference with Canvas is that in case of SVG elements, they become a part of the DOM and you can manipulate each SVG elements, whereas in case of Canvas, the entire <canvas></canvas> tag becomes one node in the DOM and anything inside the Canvas <canvas> tag (for eg. a Rectangle) cannot be manipulated individually using standard DOM methods. Browse the web and you will find a hell lot of topics on this.

Alright, now back to the detection code. Just copy/paste this inside your window load / body load or in case of JQuery the $(document).ready() method,

if(!document.createElement('svg').getAttributeNS){
  document.write('Your browser does not support SVG!');
  return;
}

So, if your browser does not support SVG, then it will not process further (in case your application cannot live without SVG). But if you have a back up plan and you can live without SVG in your browser, then you can add an else block along with the if so that you can write your backup logic.

Explanation
Well, the code is very simple. Inside the if condition I create a SVG element using the standard DOM createElement() method. Now this will create a <svg></svg> object. Now next thing I do is to check if the getAttributeNS() method is defined or not. If you browser does not support <svg> then there is no way it will understand getAttributeNS() and that’s the catch.

HTML5 Canvas Support check using JavaScript – the simple way

This is how you can check for HTML5 Canvas support in your browser by using JavaScript, there’s no need of an external library like Modernizer to do a simple check like this. In fact most of the feature checks can be done natively, that’s what I prefer.

Here is the code,

var canvasEl = document.createElement('canvas'); //create the canvas object
if(!canvasEl.getContext) //if the method is not supported, i.e canvas is not supported
{ 
  document.write("HTML5 Canvas Not Supported By Your Browser");
  return;
}

As you can see in the first line I create a HTML5 Canvas element by using createElement() DOM method. As you might know that to work with Canvas, you have to get it’s context object (2d for now), which can be retrieved by the getContext() method. So, if a browser does not support the <canvas> tag or the canvas element, then no way the getContext() method will be defined for it. That’s what I check in the second line – the if condition. Remember that a function/method name is a variable which holds a reference to the function block. So we can check whether it is defined or not, in a normal way like we do for object properties. IE8 doesn’t support HTML5 Canvas. So try this code in IE8 for a quick test.

How can I use it with my code?
Use this code block inside the window load event handler or when the DOM ready event is triggered (using document.onreadystatechange, more). You can modify it as per your need.
Leave behind a comment if you are upset with this attempt.

Augmenting the String object by extending its prototype – JavaScript

Every function in JavaScript has the prototype property which enables you to enhance the functionality of the current Object definition (the Class definition) either through adding properties or through methods. This stands true for both custom Objects as well as JavaScript defined Objects like Strings, Arrays.
Using prototypes to add functionality also has one more advantage and is performance optimized. Whatever methods or properties are defined using prototypes, they belong at the class level which means that all the instance of the Object (or rather the Class, in JavaScript there is no concept of Class, we say it Objects) will share the same copy of the method or property, rather than having their own individual copies.

In this post I am going to enhance the String Object which is already defined in JavaScript and has a lot of useful methods already available such as the split(), indexOf(), replace() etc and properties like length etc.

Now let’s add our new method to the String Object just to enhance its functionality. Let’s add a method that will remove the empty spaces between the words of a string value. For eg.

input value = The weather is very nice
output value = Theweatherisverynice

Defining our custom method
As you guessed it right we will be using the prototype property. Let’s try this thing out,

I have named our custom method as – removeEmptySpaces()

if(typeof(String.prototype.removeEmptySpaces) !== 'function'){ //you expect it to be a function if it is already there
    String.prototype.removeEmptySpaces = function(){                    
        return this.replace(/\s+/g,'');                   
    }
}

As you can see above, first we check if the removeEmptySpaces() method is already defined. If not (which is true in this case since we are adding out custom method. But the check is good to have if you do not want to overwrite a predefined method) then we define it inside the if block. So using the prototype property we add the custom method and from now on it is available to all the instances of the String Object. Inside the method definition we have a String.replace() method implemented. This will remove every occurrence of space between the words and will join them. I am using a regular expression inside the replace() method to match the spaces between the words. Once the match is found, it will be removed. You can also see the use of this keyword. This will represent the current instance that is calling our custom method – removeEmptySpaces(). 
You can learn more about the replace() method from w3schools.com

Putting it to test
Now let’s use our new method and see if it is actually doing the stuff that we wanted.

 var testStr = new String("The weather is very nice");   //create a new instance
 var newStr = testStr.removeEmptySpaces(); //call our custom method
 alert("New string after removing empty spaces is : " + newStr);

First we create a String Object instance – testStr and pass it a value. Then we call our custom method and store the returned value in newStr. Now if you alert the new value you can see that the empty spaces between the words have been removed. Here we have seen using our customized method with a String instance. What about String literals or constants? Well, we can use for them as well. Here is an example,

  var strConstant = "I am a String constant"; //defining our string literal
  var newStrConstant = strConstant.removeEmptySpaces(); 
  alert(newStrConstant); //alerts "IamaStringconstant"

So, we have seen how using prototypes we can add new functionality to our Objects. This was a simple example that I presented. You can customize any Objects as per your need.