我在这里阅读了很多答案,但几乎每个人都给出了某种跨域框架块。
示例错误:
Uncaught DOMException: Blocked a frame with origin "null" from
访问跨域框架。
相关帖子中的答案也是如此:
Make iframe automatically adjust height according to the contents without using scrollbar?
我也不想使用像 iFrame Resizer 这样的第三方库或类似的库。
@bboydflo 的答案很接近,但我缺少一个完整的例子。 https://stackoverflow.com/a/52204841/3850405
我将width="100%" 用于iframe,但代码也可以修改为使用宽度。
这就是我解决为iframe 设置自定义高度的方法:
内嵌iframe:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description"
content="Web site" />
<title>Test with embedded iframe</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<iframe id="ifrm" src="https://localhost:44335/package/details?key=123" width="100%"></iframe>
<script type="text/javascript">
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
console.log("Got message: " + JSON.stringify(evt.data) + " from origin: " + evt.origin);
// Do we trust the sender of this message?
if (evt.origin !== "https://localhost:44335") {
return;
}
if (evt.data.type === "frame-resized") {
document.getElementById("ifrm").style.height = evt.data.value + "px";
}
}
</script>
</body>
</html>
iframe source,来自Create React App 的示例,但仅使用了HTML 和JS。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description"
content="Web site created using create-react-app" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript">
//Don't run unless in an iframe
if (self !== top) {
var rootHeight;
setInterval(function () {
var rootElement = document.getElementById("root");
if (rootElement) {
var currentRootHeight = rootElement.offsetHeight;
//Only send values if height has changed since last time
if (rootHeight !== currentRootHeight) {
//postMessage to set iframe height
window.parent.postMessage({ "type": "frame-resized", "value": currentRootHeight }, '*');
rootHeight = currentRootHeight;
}
}
}
, 1000);
}
</script>
</body>
</html>
setInterval 的代码当然可以修改,但它非常适用于动态内容。 setInterval 仅在内容嵌入 iframe 时激活,postMessage 仅在高度更改时发送消息。
您可以在此处阅读有关 Window.postMessage() 的更多信息,但该描述非常符合我们想要实现的目标:
window.postMessage() 方法可以安全地启用跨域
Window 对象之间的通信;例如,在一个页面和一个
它产生的弹出窗口,或在页面和嵌入的 iframe 之间
在里面。
通常情况下,不同页面上的脚本是允许相互访问的
当且仅当它们源自的页面共享相同的协议,
端口号和主机(也称为“同源策略”)。
window.postMessage() 提供了一种受控机制来安全地
绕过这个限制(如果使用得当的话)。
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
如果您想要 iframe 的 100% 宽度和高度,我会这样做:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description"
content="Web site" />
<style>
body {
margin: 0; /* Reset default margin */
}
iframe {
display: block; /* iframes are inline by default */
background: #000;
border: none; /* Reset default border */
height: 100vh; /* Viewport-relative units */
width: 100vw;
}
</style>
<title>Test with embedded iframe</title>
</head>
<body>
<iframe src="https://localhost:44335/package/details?key=123"></iframe>
</body>
</html>
来源:
https://stackoverflow.com/a/27853830/3850405