如何在 iframe javascript 代码中获取 iframe 属性
<div>
<iframe src="http://192.168.0.108:8092/"
name="otherDomain"
width="100%"
height="600px"
style="border: 0;"
data-auth-token="xyz"
data-auth-key="abc"
>
</div>
我正在尝试
console.log(window.frameElement.getAttribute('data-auth-token'))
如果 iframe url 和浏览器用户相同,我就可以获得 iframe 属性。如果网址不同,我会收到以下错误。
Uncaught SecurityError: Failed to read the 'frame' property from 'Window': Blocked a frame with origin "http://192.168.0.108:8092" from accessing a frame with origin "http://localhost:8092". Protocols, domains, and ports must match.
是否可以在 JavaScript 中获取跨域 iframe 属性。
最佳答案
您无法从 iframe 内部访问属性,但可以访问查询字符串。
例如:
在父 html 中:<iframe src="/x?a=1"></iframe>
在 iframe JavaScript 中:window.location.search
关于javascript - 跨域获取 iframe javascript 内的 iframe 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26094833/