简体   繁体   中英

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...

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.

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 ,效果很好!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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