Thursday, June 23, 2011

HTML5 & Detection Techniques

New Logo of  HTML5
HTML5 is the new specification of HTML 5 with lots of new feature and it is still being drafted the latest specification can be found at http://dev.w3.org/html5/spec/Overview.html .

Some of the new features are
  • Numerous new tags like (article, aside, bdo, command, datalist, details, embed, footer, hgroup, keygen, mark, meter, nav, output, progress, rp, rt, ruby, section, source, summary, time, wbr)
  • Custom tag for audio and video
  • Custom tags for images and its captions (figure, figcation)
  • Custom tag for Custom Graphics (canvas)
  • New controls for webforms like (dates, times, email, url, search, number, range, tel, color).
  • Geolocation Support.
  • Web Sockets: Socket programming for client browser.
  • New logo to identify  HTML5.
The are lots of more features in it, I am yet to go through all of them and the draft is still being updated.

Canvas element allows dynamic and scriptable rendering of 2D shapes and bitmap images. [Read More...]
SVG (scalable vector graphics is supported using canvas element). Various representation of reports are presented using graphs, pie charts, etc. canvas element with the help of SVG allows to create nice images and graphics. Javascript is required to program the SVG in canvas element.

How can I detect if the browser is supporting HTML5?
Here we have to depend on javascript. A javascript library named Modernizr available under Open Source for HTML5 and CSS3.
However, as per Mark Pilgrim('s book HTML5 Up and Running), we can employ the following simple techniques to quickly identify HTML5 compatible browser.

  1. Checking the presence of certain property on the global object(such as navigator or window object).
    • Example. checking the presence of Geolocation support.
    • function supports_geolocation() {
            return !!navigator.geolocation;
      }
  2. After creating an element dynamically, then testing the presence of a certain property to that element.
    • Example. checking the canvas support.
    • function supports_canvas() {
            return !!document.createElement('canvas').getContext;
      }
  3. After creating an element dynamically, then testing the presence of a certain method to that element. Then call the method if present  and check the value returned.
    • Example. checking the video support.

    • function supports_h264_baseline_video() {
            // checks the video support
            if (!!!document.createElement('video').canPlayType;) { return false; }
            var v = document.createElement("video"); //create element
            return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
      }
  4. After creating an element dynamically, set value to a property and then to test if it has retained the value.
    • Example: Create a element 'input' dynamically (which is of type 'text' as default). Then setting the property 'type' to other value and retesting the value of the property 'type' 
    • function supports_PropertyPersistance() {
         var i = document.createElement("input"); //default element is text
          i.setAttribute("type", "color");
          return i.type !== "text";
      }

Read More on detection technique...

Reference: http://en.wikipedia.org/wiki/HTML5
Specification - Working Draft: http://dev.w3.org/html5/spec/Overview.html
Specification - Released Working Draft: http://www.w3.org/TR/html5/

Monday, June 20, 2011

Rule of Thumb : Declaring variables

"var" is the keyword used for declaring a variable. It lets you define an number, string , datetime value or an object or function.


Example(s):

var a1;
var a2 = 10;
var a3 = 1.23;
var a4 = "hello world";
var a5 = true;
var a6 = {prop1:'some values', prop2: 'some other values', prop3 : true} ;


var a7 = new Object();
var fn1 = function() { return "hello world"};


what happens if we use in the following way
 ax=10;

Well well, the above line adds a variable name "ax" in the global scope. At a glance it can pretend to be an trivial mistake, but that is not the case, rather it is of very high importance, since such statement can make the global scope dirty and making the code fault prone and fragile.

Lets take another example for more clarity:


function sayHello(username){
  strn = "Hello " + username;
  return strn;
};


console.log( sayHello( 'kris' ) );
console.log( strn ); //=> "hello kris"

*console is a class available in almost all browsers but not in all browser for development/debugging purpose.

The above javascript function "sayHello" adds a variable "strn" to the global scope.

The following change of declaring the variable with "var" will make sure that the variable "strn" is applicable to local scope only.

Examples: 
function sayHello(username){
  var strn = "Hello " + username;
  return strn;
};


console.log( sayHello( 'kris' ) );
console.log( strn ); //=> undefined
Hence the scope of  local variable is managed correctly.

So we conclude this post with the memoirs of golden rule of variable declaration in javascript.

Tuesday, June 14, 2011

Beginning Javascript

The blog of mine is making a focus shift from blogging to javascript preaching.

JavaScript was earlier called "LiveScript".
you will get a good information on javascript history here wikipedia:javascript

My inception of javascript was like simple validations for web pages in 2005. Beginning 2007, I understood that javascript has some more potential. Errie, whats the potential. Well well, you can use javascript as a full blown applications. During june-2008 I wrote my first javascript class. Yessss my first javascript class.

well here it goes.

Code:



var A= function(){
  this.sayHello = function(){
    alert('hello world');
  };
};
var o = new A();
o.sayHello();


Yes it is as simple as written here.

More is on way to you.

There was an error in this gadget