我无法调用 postMessage,以防我在 srcdoc 属性中传递 html
without sandbox = "allow-scripts"
,它给出以下错误
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://example.com') does not match the recipient window's origin ('https://example.com:444').
with sandbox = "allow-scripts"
,它给出以下错误
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://example.com') does not match the recipient window's origin ('null').
我想在没有沙箱属性的情况下调用postMessage,可以吗?
如果没有,还有其他办法吗?
你当然可以。
您的错误消息抱怨的问题是您确实设置了target-origin
的参数Window.postMessage( message, target-origin, transferable )
为无效值。
您没有显示您的代码,但如果我必须猜测,我会说您根本没有设置它,因此它默认为您当前页面的来源。
由于您的目标框架没有src
but an srcdoc
属性,它的位置将是about:srcdoc
它的起源将是null
,因此除"*"
对于postMessage的target-origin
将使请求失败。
您必须将其设置为"*"
.
frame.onload = (e) => {
frame.contentWindow.postMessage( 'hello frame', '*' );
};
<iframe id="frame" srcdoc="
<html>
<body>
<pre id='log'></pre>
<script>
const log = document.getElementById( 'log' );
onmessage = (e) => log.textContent = e.data;
</script>
</body>
</html>
"></iframe>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)