开发者

can I catch exception of Iframe in parent window of Iframe

开发者 https://www.devze.com 2023-03-12 17:10 出处:网络
I have an IFrame in a page and IFrame has some JavaScript. At run time JavaScript in IFrame gives exception which i want to catch on parent window. How to do that?

I have an IFrame in a page and IFrame has some JavaScript. At run time JavaScript in IFrame gives exception which i want to catch on parent window. How to do that?

<html> 
<head> 
<script type="text/javascript"> 
var frm123 = document.getElementById("frm123"); 
frm123.contentWindow.onerror = function() { 
    alert('error caught'); 
} 
function loadData() { 
    var oRTE = document.getElementById("frm123").contentWindow.document;
    oRTE.open(); 
    oRTE.write(txt123.value);
    oRTE.clos开发者_开发技巧e();
} 
</script> 
</head> 
<body> 
<input type="button" onclick="loadData();" value="load"> 
<input type="text" id="txt123"> 
<iframe id = "frm123" > 
</body> 
</html>


The answer depends on whether or not you have control of the iframe code, and whether or not it is the same domain.

If same domain then you can do the following to set the error handling function from the wrapping document:

document.getElementById("myiframe").contentWindow.onerror=function() {
    alert('error!!');
    return false;
}

make sure you wait for the iframe to finish loading before setting the error handler.

If it's not the same domain but you have control of the iframe content (both domains are under your control), you can communicate with the outer frame by using a cross domain communication framework (google it or build it yourself), i.e. catch the error in the iframe by setting the onerror handler from within the iframe and send it through the framework to the outer document.

If it's not the same domain and you don't have control of the iframe, there's no way for the outer document to know what's going on inside it because of security constraints.


There may be few possible causes for .onError() function not to work it may be because of cross domain URL's for this you should implement a check before actulally laoding the iFrame and for that use

var url = "google.com"
var loading_url = "/empty.html"
document.getElementById("iframe").src = loading_url;
$.ajax({
url: url,
type: 'GET',
complete: function(e, xhr, settings){
     if(e.status === 200){
          document.getElementById("iframe").src = url;
     }
  }
});

This will not work cross domain, the status code is 0 in those cases.

One more thing to check is .onError() handler please check from below sample if it matches your conditions

How can I handle errors in loading an iframe?

There are many other options or tweaks to handle this kind of errors :

if (frameDoc.title == 'title of page after expection occur')

will also do the work using on .onLoad() event of iFrame.


The only way is if the frame is on the same domain, in which case you can do, from parent:

iframe.contentWindow.onerror = function(){
    // handle error
}
0

精彩评论

暂无评论...
验证码 换一张
取 消