Title: detecting when iframe fails to load / custom 404 document
Tags: javascript;html;iframe
Question: I have a local project (just files, no server) and I am trying to either detect when an iframe fails to load, which has been unsuccessful, or change the default &quot;it may have been moved or deleted&quot; error screen to my own.
The files that are failing to load in the iframes are supposed to be missing, and i'm trying to make the iframes load &quot;[CLASSIFIED]&quot; instead of the 'sad document' chrome gives me.
all my iframes look like this:
<iframe src=&quot;./articles/0.html&quot; sandbox=&quot;allow-same-origin allow-scripts allow-top-navigation&quot;&gt;</iframe&gt;
I've tried onerror=&quot;this.src = './err.html'; &quot; but it never triggers.
I've tried making the embedded file repeatedly postMessage() to the parent page but then I can't figure out which is which.
I've even tried hacky tings like errors loading faster than files but I still fail.
Does anyone have some miracle solution to this problem?
Thanks in advance.
Comment: You really should run a local server.....

Comment: @epascarello I switched to an electron app 3 hours after asking this question haha.


Here is another answer: You wont be able to do this from the client side because of the Same Origin Policy set by the browsers. You wont be able to get much information from the iFrame other than basic properties like its width and height.
Also, google sets in its response header an 'X-Frame-Options' of SAMEORIGIN.
Even if you did an ajax call to google you wont be able to inspect the response because the browser enforcing Same Origin Policy.
So, the only option is to make the request from your server to see if you can display the site in your IFrame.
So, on your server.. your web app would make a request to www.google.com and then inspect the response to see if it has a header argument of X-Frame-Options. If it does exist then you know the IFrame will error.
Comment for this answer: thanks, the iframe thing was a hacky workaround to the function I now use, which you gave me in my other question.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:01/31/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><html><iframe>', 'Title': 'detecting when iframe fails to load / custom 404 document', 'ViewCount': 250, 'id': 65982698, 'user_AboutMe': None, 'user_AccountId': 18909112.0, 'user_DisplayName': 'uncannyorange', 'user_Reputation': 63.0, 'user_Views': 29.0, 'user_id': 13795580.0}}