Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created November 27, 2020 10:12
Show Gist options
  • Save wizardnet972/c25e697423697738a97186d9d45ab672 to your computer and use it in GitHub Desktop.
Save wizardnet972/c25e697423697738a97186d9d45ab672 to your computer and use it in GitHub Desktop.
catch error in iframe
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<iframe src="./iframe.html"></iframe>
<script>
var w = document.getElementsByTagName("iframe")[0].contentWindow;
w.addEventListener('error', function() {
console.log('in error');
});
</script>
</body>
</html>
iframe.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
im iframe!!!
<script>
setTimeout(function () {
throw "error";
}, 5 * 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment