我在http://a.com
域上有一个父窗口,这个窗口打开window.open
域http://b.com
上的新窗口。现在,第二个(子)窗口上的JS尝试访问它的父属性,如:
var api = {}
if(window.opener.parentProp) { // essential to check for this property
api = window.opener
} else {
api = window
}
生成以下跨源错误:
Uncaught DOMException: Blocked a frame with origin "https://b.com" from accessing a cross-origin frame.(…)
我已经找到了以下解决方案
try {
if(window.opener.parentProp) { // essential to check for this property
api = window.opener
} else {
api = window
}
} catch(e) {
api = window
}
但是,try
和catch
会阻止,对吗? :D所以问题是,你是否熟悉更优雅的解决方案,如:
if(window.opener.breakingCrossOriginPolicy) // just a simple property name for example purposes