开发者社区> 问答> 正文

js iframe跨域 jquery html?报错

我这里有一组静态html页面  其中a.html页面时主页面,里面有一个iframe,用来引入其他的html页面。在其他的html里面设置iframe的高度:

window.onload = function(){
    if ($('#content_frame', parent.document).length > 0) {
        $('#content_frame', parent.document).height($('body')[0].clientHeight);
    }
}

当我从文件夹里面打开a.html。然后调整iframe的src。在运行到上面的代码时候会报错,错误信息:

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

希望高手解答下原因。

展开
收起
爱吃鱼的程序员 2020-06-20 16:35:45 953 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    其他域的html不能操作a.html域的dom,需要在a.html这个域建一个b.html,把操作的js写进这个b.html里面(parent.parent.document),然后被引入的其他html页面里边嵌套一个看不到的iframe b.html.
    回复<aclass='referer'target='_blank'>@回忆若风:你得在web容器里运行吧我这边是直接从文件夹启动的没有通过ide工具悲剧了兄台你这路走不通啊因为domain=null,

    2020-06-23 13:25:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载