Access cross-domain iframe elements using JavaScript

前端 未结 1 1776
傲寒
傲寒 2021-01-03 08:03

I have an iframe in my html page and I want to access some content from the page opened in this iframe.

1条回答
  •  囚心锁ツ
    2021-01-03 08:06

    You should first read about the browser's same-origin policy. This prevents javascript from a frame or window or iframe in one origin from accessing the content or scripts in another frame in a different origin. So, if your iframe is not the same origin as the page of your script, then it cannot directly access the iframe's contents.

    If you have one of the latest browsers and you code both frames to cooperate (which means you have to control the javascript code in both frames), there is a new feature called window messaging that can be used to pass information between frames from different origins.

    0 讨论(0)
提交回复
热议问题