V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ohyeahhh

关于嵌入的 iframe 获取高度的问题

  •  
  •   ohyeahhh · Jun 30, 2022 · 1373 views
    This topic created in 1400 days ago, the information mentioned may be changed or developed.
    项目中需要用到 iframe 去加载内容的静态化网页,在设置高度时遇到了跨域的问题
    控制台报错如下:
    ( DOMException: Blocked a frame with origin "http://localhost:8088" from accessing a cross-origin frame.)
    百度后得到的结论是我在父页面与 iframe 有交互时如果不在同源下就会报这个错误,我的目的是要拿到 iframe 的高度,所以在父页面要通过 js 获取 iframe 对象,此时就报跨域了
    之前的解决是通过自己的 nginx 代理就不会跨域,但是因为某种原因这个方案不能用了,所以要寻求新的解决方案
    求各位大佬不吝赐教~
    2 replies    2022-06-30 13:34:12 +08:00
    liushuigs
        1
    liushuigs  
       Jun 30, 2022
    可以用 postMessage 将 iframe 的高度传给父窗口。这是个示例:https://runjs.qingting.work/#/projects/949aac975ff44134
    mxT52CRuqR6o5
        2
    mxT52CRuqR6o5  
       Jun 30, 2022 via Android
    如果你能改 iframe 里的代码就能做到
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2757 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 14:19 · PVG 22:19 · LAX 07:19 · JFK 10:19
    ♥ Do have faith in what you're doing.