常见问题主题 - 如何找到浏览器窗口的大小? [英] FAQ Topic - How do I find the size of a browser window?

查看:58
本文介绍了常见问题主题 - 如何找到浏览器窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

----------------------------------------------- ------------------------

常见问题解答主题 - 如何找到浏览器窗口的大小?

---------------------------------------------- -------------------------


NN支持的地方:(> NN4.0)


var winWidth = window.innerWidth;

var winHeight = window.innerHeight;


IE支持的地方: (> IE4.0)


var winWidth = document.body.clientWidth;

var winHeight = document.body.clientHeight;


在CSS1Compat模式下使用IE6(使用正式DOCTYPE):


var winWidth = document.documentElement.clientWidth

var winHeight = document.documentElement.clientHeight


组合:


var winWidth,winHeight,d = document;

if(typeof window.innerWidth!=''undefined''){

winWidth = window.innerWidth;

winHeight = window.innerHeight;

} else {

if(d.documentElement&&

typeof d.documentElement.clientWidth!=''undefined''&&& br />
d.documentElement.clientWidth!= 0){

winWidth = d.documentElement.clientWidth

winHeight = d.documentElement.clientHeight
} else {

if(d.body&&

typeof d.body.clientWidth!=''undefined''){<

winWidth = d.body.clientWidth

winHeight = d.body.clientHeight

}

}

}

http://msdn.microsoft.com/workshop/a...lientWidth.asp

http://docs.sun.com/source/816-6408-...ow.htm#1202446

http://msdn.microsoft.com/workshop/a.../measuring.asp

===

发布信息比如每天自动发送一次。他们的b
目标是回答重复的问题,并将内容提供给社区进行持续评估/改进。完整的

comp.lang.javascript常见问题解答位于 http:/ /www.jibbering.com/faq/

FAQ工作人员是一群志愿者。

-----------------------------------------------------------------------
FAQ Topic - How do I find the size of a browser window?
-----------------------------------------------------------------------

Where supported in NN: (>NN4.0)

var winWidth = window.innerWidth;
var winHeight = window.innerHeight;

Where supported in IE: (>IE4.0)

var winWidth = document.body.clientWidth;
var winHeight = document.body.clientHeight;

When using IE6 with in CSS1Compat mode (with a Formal DOCTYPE):

var winWidth = document.documentElement.clientWidth
var winHeight = document.documentElement.clientHeight

Combined:

var winWidth, winHeight, d=document;
if (typeof window.innerWidth!=''undefined'') {
winWidth = window.innerWidth;
winHeight = window.innerHeight;
} else {
if (d.documentElement &&
typeof d.documentElement.clientWidth!=''undefined'' &&
d.documentElement.clientWidth!=0) {
winWidth = d.documentElement.clientWidth
winHeight = d.documentElement.clientHeight
} else {
if (d.body &&
typeof d.body.clientWidth!=''undefined'') {
winWidth = d.body.clientWidth
winHeight = d.body.clientHeight
}
}
}

http://msdn.microsoft.com/workshop/a...lientWidth.asp

http://docs.sun.com/source/816-6408-...ow.htm#1202446

http://msdn.microsoft.com/workshop/a.../measuring.asp
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

推荐答案

FAQ server写道:
FAQ server wrote:

------------------------ -----------------------------------------------

常见问题解答主题 - 如何找到浏览器窗口的大小?

----------------------- ------------------------------------------------
-----------------------------------------------------------------------
FAQ Topic - How do I find the size of a browser window?
-----------------------------------------------------------------------



这是一个糟糕的标题,作为浏览器窗口可能比身体大得多

内容大小。


此外,发布的解决方案不如以下那样强大,来自
http://www.javascripttoolbox.com/lib/util/


//获取整个文档的宽度

// ---------------------- ----------------------------------------------

screen.getDocumentWidth = function(){

var width = 0;

var body = screen.getBody();

if(!document.compatMode || document.compatMode ==" CSS1Compat"){

var rightMargin = parseInt(CSS.get(body,''marginRight''),10)|| 0;

var leftMargin = parseInt(CSS.get(body,''marginLeft''),10)|| 0;

width = Math.max(body.offsetWidth + leftMargin + rightMargin,

document.documentElement.clientWidth);

}

else {

width = Math.max(body.clientWidth,body.scrollWidth);

}

if( isNaN(宽度)||宽度== 0){

width = screen.zero(self.innerWidth);

}

返回宽度;

};


//获取整个文档的高度

// -------- -------------------------------------------------- ----------

screen.getDocumentHeight = function(){

var body = screen.getBody();

var innerHeight =

(已定义(self.innerHeight)&&!isNaN(self.innerHeigh t))?self.innerHeight:0;

if(! document.compatMode || document.compatMode ==" CSS1Compat"){

var topMargin = parseInt(CSS.get(body,''marginTop''),10)|| 0;

var bottomMargin = parseInt(CSS.get(body,''marginBottom''),10)|| 0;

返回Math.max(body.offsetHeight + topMargin + bottomMargin,

document.documentElement.clientHeight,

document.documentElement.scrollHeight ,screen.zero(self.innerHeight));

}

返回Math.max(body.scrollHeight,body.clientHeight,

screen .zero(self.innerHeight));

};


-

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com

This is a bad title, as a "browser window" may be much larger than the body
content size.

Further, the posted solution is not as robust as the following, from
http://www.javascripttoolbox.com/lib/util/

// Get the width of the entire document
// --------------------------------------------------------------------
screen.getDocumentWidth = function() {
var width = 0;
var body = screen.getBody();
if (!document.compatMode || document.compatMode=="CSS1Compat") {
var rightMargin = parseInt(CSS.get(body,''marginRight''),10) || 0;
var leftMargin = parseInt(CSS.get(body,''marginLeft''), 10) || 0;
width = Math.max(body.offsetWidth + leftMargin + rightMargin,
document.documentElement.clientWidth);
}
else {
width = Math.max(body.clientWidth, body.scrollWidth);
}
if (isNaN(width) || width==0) {
width = screen.zero(self.innerWidth);
}
return width;
};

// Get the height of the entire document
// --------------------------------------------------------------------
screen.getDocumentHeight = function() {
var body = screen.getBody();
var innerHeight =
(defined(self.innerHeight)&&!isNaN(self.innerHeigh t))?self.innerHeight:0;
if (!document.compatMode || document.compatMode=="CSS1Compat") {
var topMargin = parseInt(CSS.get(body,''marginTop''),10) || 0;
var bottomMargin = parseInt(CSS.get(body,''marginBottom''), 10) || 0;
return Math.max(body.offsetHeight + topMargin + bottomMargin,
document.documentElement.clientHeight,
document.documentElement.scrollHeight, screen.zero(self.innerHeight));
}
return Math.max(body.scrollHeight, body.clientHeight,
screen.zero(self.innerHeight));
};

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


Matt Kruse写道:
Matt Kruse wrote:

FAQ server写道:
FAQ server wrote:

---------------- -------------------------------------------------- -----

常见问题解答主题 - 如何找到浏览器窗口的大小?

--------------- -------------------------------------------------- ----- -
-----------------------------------------------------------------------
FAQ Topic - How do I find the size of a browser window?
-----------------------------------------------------------------------



这是一个糟糕的标题,作为浏览器窗口。可能比身体内容大小大得多。


This is a bad title, as a "browser window" may be much larger than
the body content size.



正文内容大小与任何代码考虑无关

报告客户区维度。客户区维度可能不是浏览器的寡头维度,但两者密切相关。

The body content size is irrelevant to any consideration of code
reporting the client area dimensions. The client area dimensions may
not be the browser widow dimensions but the two are closely related.


此外,发布的解决方案不是如下所示,...
Further, the posted solution is not as robust as the following, ...



< snip>


您使用英语有时会令人惊讶。 跟随

不完整,因此根本不会执行。 跟随

也被编程为在浏览器

环境的各种排列中出错,包括已知环境。因此,在尝试执行不同且无关的
任务以及在常见问题解答代码不会丢失的情况下,这比常见问题解答代码更强大

。 />

我想你的''主要是作品,它不是用户'

错误''的态度是适当使用的' ;鲁棒" ;.它不是一个

标签,我会应用于没有遵循

逻辑的代码,至少可以预期它总是

从方法调用返回而不会出错。


Richard。

<snip>

Your use of English can be astonishing at times. The "following" is
incomplete and so will not execute at all as presented. The "following"
is also programmed to error-out in various permutations of browser
environments, including known environments. So this is more "robust"
than the FAQ code in the sense of attempting a different and unrelated
task and falling over where the FAQ code would not.

I suppose with your ''mostly works and where it doesn''t its the user''s
fault'' attitude that is an appropriate use of "robust". It is not a
label I would apply to code that did not follow through the logic of
the task to the point where it at least could be expected to always
return form its method calls without erroring-out.

Richard.


Richard Cornford写道:
Richard Cornford wrote:

跟随

不完整,因此根本不会执行。
The "following" is
incomplete and so will not execute at all as presented.



这就是我发布网址的原因。发布的代码只显示了使用的一般

算法。

我可能应该更清楚一点。

That''s why I posted the url. The posted code only showed the general
algorithm used.
I probably should have been more explicit about that.




" follow"还编程为各种排列错误输出

的浏览器环境,包括已知环境。所以这是b $ b更多强大而不是常见问题解答代码在尝试一个不同的和不相关的任务的意义上,并且落在FAQ代码

不会的地方。
The
"following" is also programmed to error-out in various permutations
of browser environments, including known environments. So this is
more "robust" than the FAQ code in the sense of attempting a
different and unrelated task and falling over where the FAQ code
would not.



没有人是完美的,我更愿意检查我的代码并修复它

如果你提供一个例子或描述你认为它会失败的地方。

No one is perfect, and I am more than willing to inspect my code and fix it
if you provide an example or a description of where you think it will fail.


我想你的''主要是作品,它不是它的用户'

fault''态度是适当使用健壮的。
I suppose with your ''mostly works and where it doesn''t its the user''s
fault'' attitude that is an appropriate use of "robust".



这从来就不是我的态度。

That''s never been my attitude.


这不是一个

标签我会申请代码没有按照

的逻辑完成任务,至少可以预期它总是从b
返回方法调用没有错误输出。
It is not a
label I would apply to code that did not follow through the logic of
the task to the point where it at least could be expected to always
return form its method calls without erroring-out.



再次,如果您对发布的代码看到的

问题有所了解,那么对每个人都会更有帮助。


-

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox .com


这篇关于常见问题主题 - 如何找到浏览器窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆