岁月无痕

岁月-人生
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Get window width and height

Posted on 2008-05-14 13:10  岁月无痕  阅读(354)  评论(0)    收藏  举报

Get the window width and height in JavaScript using a cross-browser function that works with all versions of Internet Explorer and Firefox.



  1. function GetWidth()
  2. {
  3.         var x = 0;
  4.         if (self.innerHeight)
  5.         {
  6.                 x = self.innerWidth;
  7.         }
  8.         else if (document.documentElement && document.documentElement.clientHeight)
  9.         {
  10.                 x = document.documentElement.clientWidth;
  11.         }
  12.         else if (document.body)
  13.         {
  14.                 x = document.body.clientWidth;
  15.         }
  16.         return x;
  17. }
  18.  
  19. function GetHeight()
  20. {
  21.         var y = 0;
  22.         if (self.innerHeight)
  23.         {
  24.                 y = self.innerHeight;
  25.         }
  26.         else if (document.documentElement && document.documentElement.clientHeight)
  27.         {
  28.                 y = document.documentElement.clientHeight;
  29.         }
  30.         else if (document.body)
  31.         {
  32.                 y = document.body.clientHeight;
  33.         }
  34.         return y;
  35. }