Archive

Archive for March, 2011

logical NOT NOT operator (!!) ?

March 24th, 2011 prakash No comments

The logical NOT operator can also be used to convert a value into its Boolean equivalent. By using two NOT operators in a row, you can effectively simulate the behavior of the Boolean() casting function.
The first NOT returns a Boolean value no matter what operand it is given. The second NOT negates that Boolean value and so gives the true Boolean value of a variable. The end result is the same as using the Boolean() function on a value.
An Example:
alert(!0); //true
alert(!!0); //false
alert(!NaN); //true
alert(!!NaN); //false

Categories: Uncategorized Tags: ,

arguments.callee – javascript useful property

March 23rd, 2011 prakash No comments

The arguments object is an array like object that contains all of the arguments that were passed into the function. Though its primary use is to represent function arguments, the arguments object also has a property named callee , which is a pointer to the function that owns the arguments object. Consider the

following classic factorial function done with arguments.callee property:


function factorial(num){
     if (num < = 1) {
          return 1;
     } else {
          return num * arguments.callee(num-1)
     }
}

There is no longer a reference to the name “ factorial ” in the function body, which ensures that the recursive call will happen on the correct function no matter how the function is referenced.

Finding n-th level of sibling on DOM with jquery

March 20th, 2011 prakash No comments

By clicking on an element in DOM, to get the which indexed number of the sibling, you’ll need to use jquery’s .index() method which is easy and useful too.
Let’s give an example,
HTML :

 <ul>
     <li>first</li>
     <li>second</li>
     <li>third</li>
 </ul>

Javascript:

$('li').click(function () {
    alert($(this).index());
});

Enjoy jquery

Categories: Uncategorized Tags: ,

CSS and javascript hack for Safari Browser

March 16th, 2011 prakash No comments

Since the Safari and chrome browser uses same webkit, differentiating safari and google chrome browser will be a difficult part. Finally I’ve found out the solution for it,
CSS HACK: for both chrome and safari browser,

@media screen and (-webkit-min-device-pixel-ratio:0){
    #yourdiv{
        margin-left:0;
    }
} 

Javascript Hack : for both chrome and safari browser,

if(window.devicePixelRatio) {
   alert('This is safari or chrome browser');
}

Javascript Hack : for safari and only safari browser,

if (navigator.userAgent.match(/AppleWebKit/) && ! navigator.userAgent.match(/Chrome/)) {
   alert('this is safari brower and only safari brower')
}