我有一个链接到其他网站的iframe,当用户按下按钮时,会重定向到iframe内的我的网站。 我现在正在检查位置是否是我的地址和基础。问题是,当该位置仍然是另一个域时,我得到了跨域错误:
Uncaught SecurityError: Blocked a frame with origin "http://localhost" from accessing a frame with origin "https://somesite.com". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "https". Protocols must match.
我如何传递此错误?
document.getElementById("paymentIframe").addEventListener('load', function() {
var url = document.getElementById("paymentIframe").contentWindow.location.href;
if( url.indexOf('status') > -1 && url.indexOf('success') > -1 )
//i am in my domain
});
答案 0 :(得分:0)
可能最简单地捕获错误并在setTimeout(..., 10)
之后再次重试,直到它停止发生(可能有一个上限;例如,如果你继续尝试超过30秒,放弃)。
E.g:
function checkForFrame(callback) {
var timeout = Date.now() + 30000; // 30 seconds
doCheck();
function doCheck() {
var url;
try {
url = document.getElementById("paymentIframe").contentWindow.location.href;
if (url.indexOf('status') > -1 && url.indexOf('success') > -1)
callback(true);
return;
}
}
catch (e) {
}
// The frame is inaccessible or the URL doesn't match; time out?
if (Date.now() > timeout) {
// Yup
callback(false);
} else {
// Nope, try again in 10ms
setTimeout(doCheck, 10);
}
}
}