Wednesday, January 16, 2013

JavaScript: console.log() - Best Practice

JavaScript developers, must be well aware of the console.log() function to log messages to the console window.

Today, I share my experience to write a wrapper function for console.log().

Simple Code 1: Ignore log message if the console object is missing


<script type="text/javascript" language="javascript">
    window.console = window.console || { log: function (d) {} };
    
    /* write your JavaScript code below this line */
    console.log("Message: Testing Console.log");

</script>


Simple Code 2: Modified to show log message as alert if the console object is missing and the global variable for debug is set to true. You can turn it off when you publish to production.


<script type="text/javascript" language="javascript">

    var debug = true;

    window.console = window.console || { log: function (d) 
        {if(debug){alert("Sorry! Logger not available on this 
            browser. \n\n" + d);} } };


    /* write your JavaScript code below this line */
    console.log("Message: Testing Console.log");

</script>


Simple Code 3: Same as above but with JQuery.


<script type="text/javascript" language="javascript">

    var debug = true;

    $(function(){
      window.console = window.console || { log: function (d) 
        {if(debug){alert("Sorry! Logger not available on this 
            browser. \n\n" + d);} } };
    });


    /* write your JavaScript code below this line */
    console.log("Message: Testing Console.log");

</script>



More Info:

This is a best practice to avoid errors with browsers that don't handle console.log missing object by default. For example, Internet Explorer don't handle the missing object and ignores all scripts following the error. Unfortunately, IE will not throw any error on this condition and eat your time debugging and tracing for the invisible error.

Note 1: console object is not a standard object in ECMA Script. However, it is one of the most widely used objects by developers for debugging JavaScript programs.

Note 2: In Internet Explorer 8 and Internet Explorer 9, console object is exposed only when Developer Tools (Shortcut - F12) are opened for the active tab.

Happy Coding!




Thursday, January 10, 2013

Who could innovate better?

Today, I came across an activity "Search for Social Advocate" inside the organization.

When organizations around the world hunting for social media specialist and writers on job boards and social networks, I am amazed with this idea of searching the advocates inside the organization.

"Social Advocates" - Interesting term!

It is better to that somebody who understand, live and experience the organization, writes about it on the social platforms. It is a Great thought, Isn't it?

I believe it brings in better productivity and usefulness of the content shared.

I am not aware of the internals or plans for the "Social Advocates". But I believe there should be a  team of Social Advocates, who could the organization co-ordinate and form a team (Virtual?). Train them with products, policies and basic writing strategies with marketing touch. I believe, the organization by their values and policies, will never intervene on the creativity of the individuals.

The team could be formed with experts, interested people from every group and product team to share thought and happenings in the markets/industry, technology and strategies.

We could always depend on the professional marketing team for promotions through advertisements and campaign activities. But for content, definitely this is the right way. Millions can't be wrong. Content is the KING, always.

Honestly, I un-follow people and pages that kills my interest with repeats and spams.There would be no chance for such scenario here, I believe.

Organization?? Who else? It is EMC - The Strong and Innovative Storage Giant of All Times.