How to detect an error 404 in an iframe?

Member

by lizzie , in category: HTML & CSS , 22 days ago

How to detect an error 404 in an iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 21 days ago

@lizzie 

You can detect an error 404 in an iframe by using JavaScript. Here is an example code snippet that demonstrates how to do this:

1
2
3
4
5
6
7
var iframe = document.getElementById('your-iframe-id');

iframe.onerror = function() {
  if(iframe.contentDocument.documentURI == "about:blank") {
    console.log("Error 404 - Page not found");
  }
};


In this code snippet, replace 'your-iframe-id' with the actual id of your iframe element. The iframe.onerror event listener is triggered when an error occurs while loading the iframe content. Inside the event handler, we check if the documentURI of the iframe content is "about:blank", which is a common indicator of a 404 error. If it is, then we log a message to the console indicating that it is a 404 error.


This code should help you detect and handle error 404 in an iframe using JavaScript.