简体   繁体   English

Javascript —检测用户浏览器的可视区域

[英]Javascript — Detect Viewable Area of a User's Browser

I want to show the user a message showing the viewable area of their browser. 我想向用户显示一条消息,显示他们浏览器的可视区域。 I have the following javascript function for doing this... 我有以下javascript函数可以执行此操作...

function main(){
    document.write("The viewable area of your browser is about <b> " + jQuery(window.parent.document).width() + " (width) X " + jQuery(window.parent.document).height() + " (height).</b>");  
}

At least one user is reporting that this is giving the wrong values. 至少一个用户报告这给出了错误的值。 For me, this is working properly. 对我来说,这工作正常。

Do you see any weaknesses or limitations in this? 您是否看到任何弱点或限制?

EDIT: I should have mentioned that this code is in an iframe that pops up over the page that I'm wanting to find the viewable area of. 编辑:我应该已经提到,此代码是在一个iframe中,该iframe会在我要查找其可视区域的页面上弹出。

You should use 你应该用

$(window).height()

and

$(window).width()

since 以来

jQuery(window.parent.document).width()

gives you the height of the document and not of the viewport. 给您文档的高度而不是视口的高度。

You can achieve this quite simply using: 您可以使用以下方法非常简单地实现此目的:

$(window.parent).height();
$(window.parent).width();

我最终使用了parent.document.body.clientWidthparent.document.body.clientHeight ,效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM