When the iframe fails to load due to a 404 error or any other error, the 'onerror' event will be triggered, alloing you to handle the situation accordingly.
<!DOCTYPE html>
<html>
<head>
<title>IFrame Error Detection</title>
</head>
<body>
<iframe src="nonexistent-page.html" id="myIframe" width="400" height="300"></iframe>
<script>
const iframe = document.getElementById('myIframe');
iframe.onerror = function () {
console.log('Error loading iframe content.');
// Add your error handling code here, such as displaying a message to the user or taking other actions.
};
</script>
</body>
</html>