Using D3 or third party libraries with Polymer – simple example

Off late I have been exploring and working with Polymer a lot. One thing I tried recently was to create a custom element (a custom tag) that has a d3 chart integrated into it. So whenever I use this element in an application the chart renders.

For those who are not familiar with Polymer, let me tell you, it’s a game changer for the next generation of web applications. The web is getting componentized with the new standards that are coming along – Shadow DOM, custom elements, templating, HTML imports e.t.c. Polymer is a polyfill layer or as they call it a SUGARING layer on top of these web standards that allow you create custom web components easily. You do not need to go the the low level API’s to create a custom element. Just use the Polymer library and start creating your own components. Also it provides a heck of other features that can be used, such as two way data bindings. So go over to the Polymer site and check it out.

Coming back to our topic of discussion, one thing that can be a bit confusing to Polymer beginners is to use a third party javascript library with your polymer element. Here I have an example of using the D3 js charting library to create a custom chart component. Let’s call our custom element  my-custom-d3-chart. By the end of this discussion we will have a custom tag – <my-custom-d3-chart />. Yes a custom HTML tag. Our own tag. Yo!!!

Read More »

Animated growing pie chart with Rapahel JS

This is my first post after a long gap – almost one year. During this period I got busy with my regular work, family and other commitments. It was really hard to keep writing although I wanted to do that.
One of the tasks that I worked on a few months back was animating a pie chart as if it is growing from 0 to 360 degrees. And this post will share that with you. So let’s get started….
You might already be knowing that Raphael JS is a great JavaScript library to work with SVG in your web projects. So did I to create the pie chart. I am using Raphael and its features to create the pie chart and animate it. Here’s the demo link. Run this in your browser


Quick Concepts: 
Most important functionality in the growing pie chart code is defining a custom Raphael paper attribute. So here’s the concept. If you have an attribute that you want to represent as a function and add your logic to it, you can create a custom attribute. For details you can see the official reference here. But here’s how I defined a custom sector attribute that represents a pie slice sector.Read More »

JavaScript typeof and instanceof – the concept and differences

As a JavaScript developer you must have used the typeof and the instanceof operator sometime in your code to detect the the type of a variable in your program logic. And most often or not you might have been confused with the different results that show up while expecting a desired result. In this post I am gonna show you an easy technique to determine the type and instance of a variable. Let’s see some examples first.

The typeof operator

var str1 = "Hello, how are you?";
typeof str1;
Output: "string"

So, we have a string literal str1, and when we find the typeof str1, it gives “string”. Remember string is a primitive data type in JavaScript. Let’s see another example,

var str2 = new String("I am doing good. Thank you!");
typeof str2;
Output: "object"

Now, we have a String Object and we have created an instance of that Object using the new operator, which is str2. Now if we do a typeof, it gives object. Remember that in JavaScript, other than the primitive data types (and null, undefined, function), everything else is an Object. So as expected you get the type as object.

Now, let’s work with Array’s and try the same thing,

var arr1 = [2,3,4]; //defining an array literal
typeof arr1;
Output: "object"

var arr2 = new Array(4,5,6); //creating an Array Object instance
typeof arr2;
Output: "object"

Firstly, we have an Array literal – arr1 and if we do a typeof it gives “object”. Why? Since Array’s are Objects and they are not part of the primitive data set.
The next example is more obvious, we create an Array Object instance – arr2, and then do a typeof. As expected we get a “object”. So we have seen, if the typeof does not return a primitive data type (which are number, boolean, string), it will return an object. There are some special cases though, look at the bottom of this page to find out.

The instanceof operator

Now let’s take up some examples of the instanceof operator.

var str1 = "Hello, how are you?"; //defining a string literal, same as above
str1 instanceof String;
Output: false

If you see above, we have a string literal and when we do a instanceof to check if the variable is an instance of the String Object, it returns false. Since a string literal belongs to the string primitive data type. Now, consider the example below,

var str2 = new String("I am doing good. Thank you!"); //defining a String object instance
str2 instanceof String;
Output: true

Here, str2 is actually an instance, so the test returns true.
But What’s up with the Array’s though? Let’s see,

var arr1 = [2,3,4]; //defining an array literal
arr1 instanceof Array;
Output: true

var arr2 = new Array(4,5,6); //creating an Array Object instance
arr2 instanceof Array;
Output: true

Both returns true as both arr1 and arr2 are Array objects and they are not part of the primitve data set.

In fact the following test also returns true

arr1 instanceof Object;
Output: true

arr2 instanceof Object;
Output: true

This is because the Array object inherits from the more generic Object object. (Yes, you heard it right Object object!).

Still confused about finding types and instances? Here is the rule to do it easily.

Rule for the test
So, how do you guess if a variable is an instance of its Object or Type. Here is the rule,
1) First check the type of the variable using typeof.
2) Now do the instanceof test.
3) a) If the typeof test returns a primitive type – number, boolean, string. Then instanceof test will return false. Because these are just primitive type data. They are not instances of any Object.
b) But if typeof returns an object, then instanceof test will return true. So you know that you are dealing with objects and instances.
For eg.

var regexp = /d{3}/; //define a regular expression object
typeof regexp; -> "object", not a primitive type
regexp instanceof RegExp; -> true
regexp instanceof Object; -> true

Take another,

var str = "Hello, me again!";
typeof str; ->  "string" , a primitive type
str instanceof String; -> false

Now, you can think of some examples and put the rule to a test.

Special cases
1) typeof null = “object” , since null is a special value in JavaScript which represents emptyness and it does not have any type.
2) typeof undefined = “undefined”, as expected
3) typeof NaN = number, since NaN is actually a special number, although it says Not a Number. Confusing? Just keep in mind.
4) typeof Function = “function”
5) typeof xmlObj = “xml”

1) typeof operator :
2) instanceof operator:

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,

  document.write('Your browser does not support SVG!');

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.

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");

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

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.

Cooler modal popup example – how to open multiple popups, scrolling pop ups per page

Some time back I came up with a cooler modal pop up window using CSS3 and JavaScript specifically for mobile webkit browsers. The example that I presented had only a single button, a click on it would open a modal pop up window.

In this post I have a similar example but this time multiple pop ups can be opened from the same page. Opening multiple pop up’s from a single page was requested by some of my readers. And here it is. I will not go deep into explaining the bits and parts of how to create a pop up window. You can go through my previous post for that. But first let’s check a demo (meant only for web-kit based browsers):

Demo link (open in iOS or Android’s browser, you can also test this in Chrome or Safari in your computer) :

Open multiple pops from a single page

How to run this in Firefox, IE and other browsers?
For this you need to make changes in the CSS file. Add CSS3 prefixes for other browsers similarly as it is there for -webkit- . CSS3 junkies would already know what I am talking about.

How to install Phonegap Facebook plugin for iOS

This post has come out of my recent struggle to integrate the Facebook Connect plugin by Dave Johnson and make it work with my Phonegap iOS app. I had a hard time figuring things out as I was new to Phonegap iOS development. I was already doing Android apps with Phonegap, but iOS has given me some tough time. Nevertheless, around 2 days of gloomy and sad face was finally rewarded with a big smile. So, better I document it somewhere so that I do not struggle again and that’s where the inspiration for this post lies. And I must say, the official documentation is pathetic for newbies.
Alright, let’s get started. I will go step by step into the process with all details and screenshots so that it is very easy for you. In this post however, I will not be talking on how to create a Phonegap iOS app, or how to create Phonegap iOS plugins. For that you still need to look at the official documentation. I will only discuss on how we can install the Facebook plugin with a Phonegap iOS app and get started using it.
Before moving further I would like to inform you that I am using Phonegap 2.2.0 for my demo. There are new versions available – 2.5.0 being the latest at the time of writing. So you might want to check the official pages if you are using the latest version of Phonegap. But the steps mentioned below should work with the new versions of Phonegap as well. OK, time to start now.

Note: Phonegap and Cordova are the same (well, at least for me..). I prefer calling Phonegap.

1) Do I need a Mac? Simple answer – Yes, you need a Mac. I have heard and read thousand times about  people asking if iOS apps can be developed in a Windows machine. Simply, I just did not research, instead I have a Mac and I started on it. But the answer is you need a Mac definitely, since you will use XCode and the iOS SDK for development.

2) Create a Phonegap iOS App – I am using Phonegap 2.2.0. I am not going to show how to create a Phonegap iOS app. For that look at this pdf documentation here. If you cannot open it, check out this link which is the pdf source. This should get you started. However I have some screenshots below which should also help you out.
a) I have created a basic Phonegap app – FacebookPluginTest inside Cordova22FacebookTest folder under Documents. See the screenshot below. I have used the Terminal to create the app. You can find details about the command in the document above. So make sure you go through it once.
Create a basic Phonegap app with Terminal Read More »

Authoring “Adobe Edge Inspect Starter”…

My book “Instant Adobe Edge Inspect Starter” on mobile web debugging and testing has been published and it is now available online. For more details and to purchase you can visit here.

My book on mobile web debugging and testing.

Mobile web testing is currently a really time consuming and cumbersome process as there are no direct debugging tools available with mobile web browsers. Since mobile devices vary so much it is important to ensure that your web page looks as intended across the multiple mobile devices that you are targeting for your audience. Edge Inspect is a perfect tool for web developers and designers who are developing for mobile devices, allowing them to simultaneously test on numerous devices in real time as they develop without learning anything new.

“Instant Adobe Edge Inspect Starter” is a practical, hands-on guide that provides you with a number of detailed steps, which will help you to get started on testing and previewing all your mobile web projects on multiple mobile devices. This book will also show you how to use all the other available features of Edge Inspect and make the entire testing process on mobile devices very simple, effortless, and faster.

This book starts with an introduction to Edge Inspect and will take you through a number of clear and detailed steps needed to set up a working installation, and get up and running with testing your web pages on mobile devices.

You will also learn why traditional ways of testing mobile web applications are not very helpful and how Adobe Edge Inspect overcomes it. You will have a look at connecting single and multiple mobile devices with your computer and how to browse in sync.You will learn about remotely inspecting and previewing mobile web pages on a targeted device and directly see the changes taking place on the device itself. The book discusses in detail about creating your very own simple mobile web application, running it from a local server and testing it across mobile devices. You will also take a look at how to use the Edge Inspect web inspector window and do some basic HTML, CSS, and JavaScript debugging. And then finally you will have a look at using your own local Weinre debug server with Edge Inspect and some other very important features. If you want to take advantage of Adobe Edge Inspect and make mobile web testing a lot easier, then this is the book is for you.

“Instant Adobe Edge Inspect Starter” will guide you in getting started with Edge Inspect and will make testing on mobile devices a lot simpler and faster. The book is packed with a lot of examples and diagrams that will help you to test all your mobile web projects without any hassle.

Who this book is for?
This book is for frontend web developers and designers who are developing and testing web applications targeted for mobile browsers. It’s assumed that you have a basic understanding of creating web applications using HTML, CSS, and JavaScript, as well as being familiar with running web pages from local HTTP servers. Readers are also expected to have a basic knowledge of debugging web applications using developer tools such as the Chrome web inspector. And of course you need some mobile devices for running the example in this book and testing it.


Ben Forta (Adobe Systems Inc‘s Director of Developer Relations):

Creative and Code

Handy eBook to have available – by Chris.M
Overall, the book does a great job of getting you up and running with Edge Inspect. The first couple of chapters focus on getting the application installed on your computer, and then getting it set up on your mobile devices. Though the installation is fairly straightforward, the instructions they provide are detailed with plenty of screenshots, so you should have no issues getting set up.
They also give an overview of the best features of Edge Inspect. I’ve been using Edge Inspect for a while now and even I learned a few new things. The application really does make mobile testing incredibly easy with remote inspection and console log.
The eBook wraps up with some great resources for using Edge Inspect. If you are thinking of working on Responsive Web Design, Adobe Edge Inspect is an invaluable tool. I think the eBook is super handy to have as a reference, and for 5 bucks, why not?

worth having it as a reference – by Siddarth Kalyankar
“Instant Adobe Edge Inspect Starter” indeed is a starter for developers who wish to explore the possibilites of using Adobe Edge Inspector tool during their day to day development activity. It talks about Step-by-step, hands-on recipes to debug, test, and preview web applications on multiple mobile devices with Adobe Edge Inspect (Previously known as “Adobe Shadow” ).
This book assumes that the person who reads this is already into mobile web development and address some of his/her problems which are faced during the development cycle. This book is not intended to help you start doing mobile web development, but if you are web developer and willing to do mobile web development, this could come in handy for you as well.
The books briefs you about what is Adobe Edge Inspect, What are the reasons to use it and What you can do with with it. The main focus is on installation of the required components on your computer, the Edge inspect client on mobile device , how to pair mobile device with your computer and how to debug and preview. I found this information quiet useful as the entire installation process has been very well illustrated with screen shots focusing on Mac/ Windows operating systems, and Android/IOS for mobile device. The author has also provided with the code which can be used by first time developers to get started on their mobile web development venture.
If you are looking at speeding up your mobile web development, Adobe Edge Inspect is the tool and this book is worth having it as a reference.

More reviews from – Amazon

Replicating the iPhone Swipe Gesture – Vertical swiping

For those who wanted a vertical swiping feature to the the swipe gesture gallery that I created earlier, this post has a new demo and minimal explanation about a vertical swipe gesture gallery. Now you can swipe the images up or down.
I will not go through the basics once again as I have explained them in details in my previous posts. You can refer them once again in these two tutorials – post 1, post 2. Check out the demo below. Open the link in a webkit browser in either your mobile device or your computer.

Demo link:

Below is a screenshot of the gallery in action. You can see that the images are being moved vertically.

Screenshot of vertical swiping through images
Screenshot of vertical swiping through images

Read More »