javascript - Why is my iframe blocked by cross origin even it is not
问题描述
I have a <iframe>
inside a <div>
. My outer url is http://localhost:9090
and the src in iframe is http://localhost:9090/Server/v1
. In my outer context I have below code to access iframe dom:
const f1 = window.frames[iframeName];
const iFrameHead = f1.document.getElementsByTagName("head")[0];
and I get an exception on the second line:
Uncaught DOMException: Blocked a frame with origin "http://localhost:9090" from accessing a cross-origin frame.
The reason I want to access ifame dom is that I need to insert a script into that iframe. The src in iframe has the same protocol, hostname and port number. What I don't understand is the iframe is on the same origin as the parent context. Why does it complain about the cross origin?
After some debugging, I found there is because the iframe
was redirected to a different domain than its src
. I wonder whether there is a workaround to insert a script into this iframe?
解决方案
推荐阅读
- javascript - 如果使用舍入,有人会面临浮点数的简单算术运算的错误结果吗?
- sql - Oracle 查询条件
- bash - 让 Git 在 Web 主机上使用 VSCode 和文件的问题
- java - 推送到私有 Gitlab 时自动更新 Nexus Repository Manager 上的项目
- android - ViewModel 返回 ArrayList 大小为零
- html - 在闪亮中设置小部件与其标题(文本)之间的距离
- excel - 更新 Excel ListObject 标题名称而不破坏枢轴
- ios - 如何使用 APNs 密钥 (.p8) 为 iOS 应用程序配置推送通知
- r - ggplot 调用更改区域设置 LC_NUMERIC 变量
- javascript - JavaScript OOP - 符号混淆