如何使用JavaScript获得以像素为单位的浏览器宽度

How can I get the width of the browser in pixels using JavaScript?

本文关键字:为单位 浏览器 像素 何使用 JavaScript      更新时间:2023-09-26

我想根据浏览器的宽度和高度设置<iframe>的宽度。

是否可以使用JavaScript获得浏览器的宽度?

尝试使用:

$(window).width();

如果您不想使用jQuery来确定浏览器窗口的实际大小,请使用以下属性:

在Internet Explorer(怪癖模式):

document.body.offsetWidth

在Internet Explorer <9(标准模式):

document.documentElement.offsetWidth
在大多数其他浏览器中

:

window.innerWidth

你可以试试这个

 var objlast = document.getElementById('lastdiv');
 if (objlast.offsetParent) {
            do {
                  curleftLast += objlast.offsetLeft;
                  curtopLast += objlast.offsetTop;
            } while (objlast = objlast.offsetParent);
      }
    document.getElementById('iframe').style.height = curtopLast+"px";

这也可以,并且lastdiv将是您想要获取窗口高度的窗口端点。

谢谢。