can I catch exception of Iframe in parent window of Iframe

前端 未结 3 1659
名媛妹妹
名媛妹妹 2020-12-15 06:30

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?



        
3条回答
  •  萌比男神i
    2020-12-15 06:58

    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.

提交回复
热议问题