Wednesday, September 19, 2012

Console.log for IE and Chrome


<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
  <!--
  var alertFallback = true;
  if (typeof console === "undefined" || typeof console.log === "undefined") {
     console = {};
     if (alertFallback) {
      console.log = function(msg) {
              alert(msg);
         };
     }
   }
 //-->
  </script>
 </head>

 <body onload="console.log('Error Message')";>
  
 </body>
</html>

Saturday, September 15, 2012

Google image pop up effects in javascript

//Google image pop up effects:
// imgOld is Old image element
// imgNew is New Image element which is going to be show as it is
// Following function return left and top position of pop up dialog div in which we have to bind New image element

function getDivPos(imgOld,imgNew)
{
    var offset= imgOld.getClientRects();
   
   
    var left=offset[0].left-(imgNew.width/2-imgOld.width/2);
    var top=offset[0].top-(imgNew.height/2-imgOld.height/2);
   
    if((top + imgNew.height) > document.documentElement.clientHeight)
    {
    top= top - (top + imgNew.height - document.documentElement.clientHeight);
    }
    if((left + imgNew.width) > document.documentElement.clientWidth)
    {
      left=left - (left + imgNew.width- document.documentElement.clientWidth);
    }
   
    if(0> left)
    {
       left=document.documentElement.scrollLeft;
    }
    else
    {
       left=left+document.documentElement.scrollLeft;
    }
    if(0>top)
    {
      top=document.documentElement.scrollTop;
    }
    else
    {
      top=top+document.documentElement.scrollTop;
    }
   
       
    left=Math.round(left);
    top=Math.round(top);
   
    return ({"left":left,"top":top});
}

Friday, September 14, 2012

Resize image Proportionally using javascipt

function reSizeImage (imgToResize,expWidth,expHeight) {

          
            var originalImg = new Image();
            originalImg = imgToResize;

            var originalWidth = originalImg.width;
            var originalHeight = originalImg.height;
           
            // Return image if fit with the existing div without resize       
            if (originalWidth <= expWidth && originalHeight <= expHeight)
            {
                return imgToResize;
            }
            // Resize image
            var newImgSize = setSizePresetImage(expWidth, expHeight, originalWidth, originalHeight);
           
            var newImg = new Image();
            newImg= originalImg;
            newImg.width=newImgSize.width;
            newImg.height=newImgSize.height;
            return newImg;
       
    };
    function setSizePresetImage (expW, expH, orgW, orgH) {
        //Get radio
        var ratio = orgH / orgW;

        if (orgW >= expW && ratio <= 1) {
            orgW = expW;
            orgH = orgW * ratio;
        }
        else if (orgH >= expH) {
            orgH = expH;
            orgW = orgH / ratio;
        }
        //return new width and height
        return ({"width":orgW, "height":orgH});
    }