在内容页中使用JavaScript获取页面维度

Get page dimensions using JavaScript in a Content Page

本文关键字:获取 JavaScript      更新时间:2023-09-26

我正在尝试在内容页(引用母版页)中使用javascript方法来获取当前屏幕尺寸,并将其写入标签。

到目前为止,我没有运气。我尝试在母版和内容页中都这样做,但这种方法根本不起作用。

有人能提供解决方案吗?

主页:

<head runat="server">
    <title>Lake, Smit People</title>
    <!--[if IE ]>  
     <link rel="stylesheet" type="text/css" href="ProfileStypeSheetIE.css" />
    <![endif]-->  
    <!--[if !IE]>-->  
    <link href="ProfileStypeSheet.css" rel="stylesheet" type="text/css" />
    <!--<![endif]-->  
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
    <style type="text/css">
        .style1 { width: 378px; }
    </style>
</head>

Default.aspx

<asp:Content ID="Content5" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
    var height = screen.height;
    document.getElementById('<%= hiddenLabel.ClientID%>').value = height;
</script>
</asp:Content>

你好,你可以试试这些行,你的具体情况是第三个(clientHeight和clientWidth)

document.body.scrollWidth, document.body.scrollHeight // for the full size of the document
 
document.body.offsetWidth, document.body.offsetHeight //to the displayed size
 
document.body.clientWidth, document.body.clientHeight //customer for size displayed

您尝试过以下操作吗?

<a href="javascript:alert
  ('Your resolution is ' + screen.width + 'x' + screen.height);">
     Click for your screen resolution </a>