简体   繁体   English

如何在移动设备上的iframe内部找到设备高度?

[英]How can I find the device height from inside an iframe on mobile?

I have an app that is loaded inside an iframe. 我有一个在iframe中加载的应用。 The code looks something like this (sample): 代码看起来像这样(示例):

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
</head>
<body>
    <iframe src="app_url_on_a_different_domain" height=100% width=100%>
        <html>
            <head>
                <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
            </head>
            <body>...</body>
        </html>
    </iframe>
</body>
</html>

I am trying to get the viewport height from inside the iframe by doing the following (the iframe src is a different domain so I cannot access the parent container): 我正在尝试通过执行以下操作从iframe内部获取视口高度(iframe src是一个不同的域,因此我无法访问父容器):

document.documentElement.clientHeight

This gives me the correct viewport on desktop browsers; 这为我在桌面浏览器上提供了正确的视口。 however, on mobile safari/chrome, instead of getting the actual device height, I get the height of all the content inside the iframe. 但是,在移动Safari / Chrome浏览器中,我没有获取实际设备的高度,而是获取了iframe中所有内容的高度。 So instead of "460" on a iPhone 5, I get something like "3000". 因此,我得到的不是“ iPhone 5”上的“ 460”,而是“ 3000”。

How can I get the available viewport from inside this iframe? 如何从此iframe中获取可用的视口?

All modern browsers do not allow to do this from different domain(ie different origin). 所有现代的浏览器均不允许来自不同域(即不同来源)的操作。 This is the answer. 这就是答案。

Why you think what must be an alternate route? 为什么您认为必须选择替代路线?

You can get info only from what origin where is your script. 您只能从脚本的来源获取信息。 Because if you can get access for example to Screen object and any one else you can get the access to it prototype chain (via __proto__ ). 因为如果您可以访问例如Screen对象和任何其他对象,则可以访问它的prototype链(通过__proto__ )。 By this chain you can get access to Object and redefine all getters and setters for all objects, for example. 例如,通过此链,您可以访问Object并重新定义所有Object所有getter和setter。 And control from iframe the parent page. 并从iframe控制父页面。 And it will be fail, facepalm and etc. 这将是失败,facepalm等。

It is naively to find the exit from room without door. 天真地找到没有门的房间的出口。

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

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