Jer*_*rry 6 html javascript iframe
我遇到了一个令我困惑的问题.我们都知道,具有相同来源的几个浏览器窗口(选项卡)可以共享JavaScript资源.
所以,我通过父窗口打开了一个子窗口window.open.子窗口通过父窗口调用全局函数window.opener.functionName.看看下面的两个片段:
parent.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Parent</h1>
</body>
<script type="text/javascript">
function sayHi(child) {
console.log("Before throwing an error..");
throw new Error('Some error');
}
window.open('child.html')
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
child.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Child</h1>
</body>
<script type="text/javascript">
window.opener.sayHi(window);
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
问题是:你可以console.log在parent.html窗口中找到结果,但在父窗口中抛出的异常会在child.html窗口中出现奇怪的现象.这让我感到困惑,因为BDD测试,我在父窗口中需要该异常.我怎样才能实现呢?
抱歉我的英语不好,随时纠正我的语法错误.
========================更新======================
我使用HTML5功能找到了这个问题的解决方案postMessage.但我仍然好奇为什么会这样.什么使异常从父窗口转移到子窗口?任何灵感都表示赞赏.