Wednesday, November 27, 2013

JavaScript : Flash Title For New Message



Whenever you chat with your friends on Facebook or Gmail, you often see new blinking message which appears on Title of browser when you’re on other webpage or tab.

I always feel how they would notify us with this message. After some research I found that it is a small JavaScript which makes this happen.

This tutorial post will help you to flash Title of web browser for new message.

Let’s look at some JavaScript stuff

    var message_interval = null;
    var user = 'Zainul';
    var message = 'Hello There';
    var odd_even = 0;
    var old_title = document.title;

    /**
     * Show title message when new message arrives 
     */
    function showTitleMessage() {
        // set time interval
        message_interval = setInterval(function() {
            // reset title and execute next task when user returs to this tab
            $(window).bind('focus', function() {
                clearInterval(message_interval);
                document.title = old_title;
                // execute next task
                // use callback function for next task
            });

            //set message
            document.title = odd_even ? message : user + ' says...';
            // set odd or even
            odd_even = !odd_even;

        }, 2000);
    }



You can use this tutorial to show new message arrives when you are looking at message list, example Stack overflow questions, Chat message.

Hope this tutorial will help you.
Read More...

Monday, November 25, 2013

CSS: How To Create Custom Scrollbar For Webkit Supported Browsers

One of eye catching thing of Gmail new updates was its scrollbar. First I thought it would be an html element represented by some kind of Javascript. But after some time I realized that it was not Javascript effect rather it is a CSS effects for scrollbars.



One amazing thing about this scrollbars are, they can be represented in different style for different HTML elements within same webpage.

Today we will see the implementation of such scrollbar for Webkit supported browsers.

First we will look at basic CSS properties of scrollbar.

::-webkit-scrollbar             
::-webkit-scrollbar-thumb       
::-webkit-scrollbar-track       
::-webkit-scrollbar-button      
::-webkit-scrollbar-track-piece 
::-webkit-scrollbar-corner      
::-webkit-resizer

Following image shows location these properties within scrollbar.


Now let’s look at the one basic example

            body{
                min-height: 1000px;
            }
            ::-webkit-scrollbar{
                width: 10px;
                margin:5px;
            }
            ::-webkit-scrollbar-track{
                background: #fff;
                -webkit-box-shadow: inset 1px 1px 2px rgba(0,0,0,0.1);

            }
            ::-webkit-scrollbar-thumb{
                background: #aaa;
                box-shadow: 0px 0px 1px rgba(0,0,0,0.5);
            }
            ::-webkit-scrollbar-thumb:hover{
                background: #888;
            }
            ::-webkit-scrollbar-thumb:active{
                background: #444;
            }




You can also customize scrollbars of overflow HTML elements, like this
 
<div id="custom-scrollbar">
            <div class="content">
                <!-- some HTML stuff -->
            </div>
        </div>

And CSS code

#custom-scrollbar {
                width: 20%;
                height: 300px;
                overflow: auto;
                background: #eee;
            }
            .content{
                height: 600px;
            }
            
            #custom-scrollbar::-webkit-scrollbar{
                width: 10px;
                margin:5px;
            }
            #custom-scrollbar::-webkit-scrollbar-track{
                background: #fff;
                -webkit-box-shadow: inset 1px 1px 2px rgba(0,0,0,0.1);
                border-radius: 10px;
            }
            #custom-scrollbar::-webkit-scrollbar-thumb{
                background: #F77;
                box-shadow: 0px 0px 1px rgba(0,0,0,0.5);
                border-radius: 10px;
            }
            #custom-scrollbar::-webkit-scrollbar-thumb:hover{
                background: #A55;
            }
            #custom-scrollbar::-webkit-scrollbar-thumb:active{
                background: #744;
            }
Hope you would like this post.
Read More...

Wednesday, November 20, 2013

Preload Images Using Javascript


Preload Image is technique which helps browser to render images with minimum delay.

preload images

Today we will see example of image gallery. Gallery contain 5 images which load images one after another whenever user clicks next and back button.

This is a basic image gallery scenario which is used in all possible website, then why we need preloaded images for this gallery?

Now a day’s most of website becoming faster and user expectation is increasing.

Suppose your website doesn’t use preload image technique for gallery and a user visits any image gallery from Google Plus or Facebook and visits your website gallery.

Then that user always prefer those websites rather than yours. Why?

Because your website load one image at a time. When user click on next button, then only gallery load image and user has wait till it get loaded.

To avoid this situation gallery needs to download all images first so that browser cached them and when user clicks next button image get fetched from browser cache rather than image server.

Now let look at some programming related stuff

    
// first we create JSON record of images
var imageRecord = ['image1.jpg',
                'image2.jpg',
                'image3.jpg',
                'image4.jpg',
                'image5.jpg'];

// now iterate each JSON record to load image
for (var pos = 0; pos < imageRecord.length; pos++) {

    // create image object
    var image = new Image();

    // assign image path
    image.src = imageRecord[pos];

    // additional stuff
    // some time we need to perform some logic after image load
    // it is hard to remember which image is loaded in a loop 
    // therefore we assign attribute to image which identifies JSON record of that image
    image.setAttribute('img-pos', pos);
                
    // bind load event to image
    Image.onload = function() {

        // get image position
        image.getAttribute('img-pos');

        // perform operation based on fetch image index
    }
}
    
    
Note:
Browser downloads images (other static resource) parallel per hostname. Therefore we can’t predict which image get downloaded first. That is why we added the image position attribute to identify which image is loaded.

If you have only few images then you can use following code

<img src = “image 1” width=”1” height=”1” />
<img src = “image 2” width=”1” height=”1” />
<img src = “image 3” width=”1” height=”1” />

As it occupy only small part of browser and all images get downloaded and cached.

Hope this tutorial post will help you.
Read More...

Friday, November 8, 2013

How To Implement Review Rich Snippet


Rich snippet helps to create more interactive appearance of your webpage in SERP.


There are different sets of Rich snippet which are approved by Google.

Today we will see the how to implement Review – Rich snippet.

Schema.org provides vocabulary to create snippet that SERP page shows result with additional information like ratings, review count, description.

Let’s implement this snippet.

Here is sample example without snippet.

Basho’s is one of the best restaurant in Pune - by Zainul, October 1, 2013
4/5 stars
Very nice ambiance, food & service. ...!

Now we will add Shema.org vocabulary for this page.

<div itemprop="review" itemscope itemtype="http://schema.org/Review">    <span itemprop="name">Basho’s is one of the best restaurant in Pune </span> -    by<span itemprop="author">Zainul</span>,
    <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
      <meta itemprop="worstRating"content="1">
      <span itemprop="ratingValue">4</span>/
      <span itemprop="bestRating">5</span>stars
    </div>
  </div>
    <meta itemprop="datePublished" content="2013-10-01">October 1, 2013
    <span itemprop="description”>Very nice ambiance, food & service. ...!</span>

Note:
If required snippet information is not present in HTML then you can use meta tag to add snippet information. Like in above example we used meta tags for worstRating and datePublished.

Now validate you webpage in Rich snippet tools.


After all this process SERP won’t show your page with rich snippet.  What?
It will, but you need to wait till Google fetch this page, index it and validate rich snippet after that it will come in SERP.

Note:
Rich snippet does not increase page rank, rather your webpage standout in SERP by rich content.
Read More...