javascript - 加载前在 iFrame 上设置事件侦听器
问题描述
假设我在example.com
,并且我有一个 iFrame example.com
。(是的,它们是相同的)我需要捕获 iFrame 中发生的错误。如何在 iFrame 中设置事件侦听器,然后它的脚本元素开始加载?
我以前看过它,例如在repl.it上。
解决方案
你可以试试这样的 ->
<iframe id="myframe" src="https://google.ca"></iframe>
<script>
myframe.onload = function(){
var that = document.getElementById('myframe');
try{
(that.contentWindow||that.contentDocument).location.href;
}
catch(err){
//err:SecurityError: Blocked a frame with origin "http://*********" from accessing a cross-origin frame.
console.log('err:'+err);
}
}
</script>
推荐阅读
- windows - 如何在设计模式下在VB.2017 Windows窗体的CheckListBox中插入项目?
- r - 大尺寸简单功能
- c# - Xamarin 跨平台选择器未正确绑定
- c# - 使用 throw 后 C# 未进入异常
- c++ - 摇滚/纸/剪刀游戏无法正常运行
- c++ - 如何使用 boost beast 为 websocket 握手添加自定义标头
- python - 从 dask 数据框中删除有问题的列
- c - 将结构成员值存储在名称较短的本地 var 中是不好的做法吗?
- c++ - 如何在 C++ 中重命名具有“未知”名称的文件?
- javascript - 在 HTML Header 中创建可变变量