首页 > 解决方案 > 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?

标签: javascripthtmliframe

解决方案


推荐阅读