gpt4 book ai didi

javascript - 跨域但控制两个域 : Set the height of an iframe per it's contents?

转载 作者:行者123 更新时间:2023-11-30 13:01:07 25 4
gpt4 key购买 nike

我们有两个域,domain.comsub.domain.com

在 domain.com 上

<html> 
<head>
<title>Main</title>
</head>
<body>
<h1>This is the parent</h2>
<iframe src="http://sub.domain.com/results.aspx"
width="100%" height="1px" id="iframe1">
</iframe>
</body>
</html>

问题:我们如何将 iframe 高度从 1px 调整到它的实际高度(当 iframe 加载时)?当用户在 iframe 中播放时,它的高度可能会发生变化,所以我怀疑可能需要将它挂接到某个事件。

现在我完全意识到跨脚本在浏览器中是被阻止的,甚至子域(或不同的端口号)也构成“跨域”。但是,我们控制着这两个域,因此我正在为此寻找一个好的解决方案。我们看了window.postMessage这似乎是专门为此设计的,但无法使其工作。

最佳答案

您的情况很简单,因为它是跨脚本子域(而不是跨脚本不同域)。只需在您的页面和 iframe 中分配 document.domain = "domain.com"。有关更多信息,请查看: Cross sub domain iframes and JavaScriptHow to communicate between frames?

关于javascript - 跨域但控制两个域 : Set the height of an iframe per it's contents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com