1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android2系でscript読み込みが失敗するとwindow.onerrorハンドラが実行される

1
Posted at

タイトルが全てです。以下補足です。

Android2系に限ってscriptタグが失敗するとwindow.onerrorが発生します。

(Android2系の情報は今更感ありますが…。)

<head>
<script>
window.onerror = function(){
  alert('window onerror'); // Android2の場合のみ実行される
}
</script>
<script src="noexist.js"></script> <!-- 存在しないjsファイル -->
</head>

動的にscriptタグを追加した場合も同様です。

window.onload = function() {
	var head = document.getElementsByTagName('head')[0];
	var script = document.createElement('script');
	script.setAttribute('src', 'noexist.js');
	script.onerror = function(){
		alert('script onerror'); // どのブラウザでも実行される
	}
	head.appendChild(script);
}
window.onerror = function(){
	alert('window onerror'); // Android2系のみ実行される
}

scriptタグの読み込み失敗時にwindow.onerrorが実行されるのは自然のようにも思えますが、Android3系からは他のブラウザに合わせたのか、window.onerrorは実行されなくなったようです。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?