Search code examples
javascriptjquerywindowcenter

get center of width in all browsers with jquery


i have following code to get center of width

  function alertSize() {
     var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth ||        document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
   //IE 4 compatible
   myWidth = document.body.clientWidth;
  }

return myWidth/2;
}

but it did't return correct center of screen.

what i need to get correct center horizontal.

thanks


Solution

  • Try this:

    function alertSize() {
     return $(window).width()/2
    }
    

    jQuery has already done all the extra work for you.