0

I have JS script which loads the contents of a webpage inside an iframe like this:

$("#iframeid").attr( "src", url );

Now, there is a security error being thrown on the webpage at url, due to cross-origin violation. The webpage has some JS code which tries to access window.parent and fails because of cross-origin. Other than that the webpage loads fine.

I want to be able to catch this error in my script (and simply log it). I am trying to use try-catch as follows:

try {
    $("#iframeid").attr( "src", url );
}
catch(err)
{
    console.log(err);
}

but it does not catch the error. My guess is that the try code finishes executing before the webpage is loaded.

Is there a way for me to catch this error on my side at all? Thanks.

wyaneva
  • 23
  • 5
  • Just a note to confirm your suspicion on the outcome of the page load not being concluded in sync with the try-catch. Page loading is an async process, and the sequence of arrival of the assets mentioned in the page has no specific order unless you work really hard to achieve that. Identifying a load error might be possible by looking at the page headers, which itself has been asked before - see https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript. – Vanquished Wombat Jan 28 '20 at 12:08
  • Apparently you can do a HEAD request via Ajax which might be a viable approach, as in, you do the head request in your main page and see what you get - if you get a CSS error then you don't do the iframe load. Format for the get is xmlhttp.open("HEAD", the_url, true); – Vanquished Wombat Jan 28 '20 at 12:15
  • Hi, thank you for your comments. The error is not from CSS, it is from a JS script on the webpage, which tries to resize `parent.window`, but fails due to cross-origin. This is expected behaviour and other than that the webpage loads just fine. So I simply want to catch this error and ignore it on my side. – wyaneva Jan 28 '20 at 12:21

1 Answers1

1

Looks like asked before - see How to catch JavaScript errors in all Iframes (with window.error)?

Seems that you can catch the script error using a window.onerror() listener attached to the iframe.

General info on window.onerror() listener at MDN here.

Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
  • Thanks, I was not aware of `window.onerror`. It doesn't seem to catch my error though, and that might be because of the cross-origin issue - apparently `onerror` only works when both pages are in the same domain: https://stackoverflow.com/questions/6327128/can-i-catch-exception-of-iframe-in-parent-window-of-iframe – wyaneva Jan 28 '20 at 14:09
  • Hmm - ok sorry to jump to conclusions. It may be that cross-site-scripting (what I meant by CSS) protection in browsers via same-origin policy is going to be difficult to work with. Good luck! – Vanquished Wombat Jan 28 '20 at 14:27
  • No worries, thank you for your suggestions. Yes, that seems to be the case that you are not able to catch errors like this due to security restrictions. As, luckily, my script was a part of a larger Qt application, I ended up catching the error inside this. – wyaneva Jan 28 '20 at 16:04