質問するログイン新規登録

回答編集履歴

4

event-typeの修正(onload→onunload)

2015/08/25 02:26

投稿

think49
think49

スコア18194

answer CHANGED
@@ -78,7 +78,7 @@
78
78
  removeEventListener('unload', handleUnload, false);
79
79
  } else if (document.detachEvent) {
80
80
  document.detachEvent('onclick', handleClick);
81
- detachEvent('onloaded', handleDOMContentLoaded);
81
+ detachEvent('onload', handleDOMContentLoaded);
82
82
  detachEvent('onunload', handleUnload);
83
83
  }
84
84
  }
@@ -91,7 +91,7 @@
91
91
  } else if (document.attachEvent) {
92
92
  document.attachEvent('onclick', handleClick);
93
93
  attachEvent('onload', handleDOMContentLoaded);
94
- attachEvent('onload', handleUnload);
94
+ attachEvent('onunload', handleUnload);
95
95
  }
96
96
  }
97
97
 

3

initEvent(); の実行漏れ修正

2015/08/25 02:26

投稿

think49
think49

スコア18194

answer CHANGED
@@ -50,6 +50,8 @@
50
50
  currentTarget.attachEvent('onclick', handleClick);
51
51
  }
52
52
  }
53
+
54
+ initEvent();
53
55
  }());
54
56
  ```
55
57
 
@@ -92,5 +94,7 @@
92
94
  attachEvent('onload', handleUnload);
93
95
  }
94
96
  }
97
+
98
+ initEvent();
95
99
  }());
96
100
  ```

2

変数名の修正(target→currentTarget)

2015/08/25 01:12

投稿

think49
think49

スコア18194

answer CHANGED
@@ -40,14 +40,14 @@
40
40
  }
41
41
 
42
42
  function initEvent () {
43
- var target = document.getElementById('sample');
43
+ var currentTarget = document.getElementById('sample');
44
44
 
45
- getCurrentTarget = createGetElement(target);
45
+ getCurrentTarget = createGetElement(currentTarget);
46
46
 
47
- if (target.addEventLisetner) {
47
+ if (currentTarget.addEventLisetner) {
48
- target.addEventLisetner('click', handleClick, false);
48
+ currentTarget.addEventLisetner('click', handleClick, false);
49
- } else if (target.attachEvent) {
49
+ } else if (currentTarget.attachEvent) {
50
- target.attachEvent('onclick', handleClick);
50
+ currentTarget.attachEvent('onclick', handleClick);
51
51
  }
52
52
  }
53
53
  }());

1

event.currentTargetを返す関数コード、window.onunload のコード追加

2015/08/25 00:53

投稿

think49
think49

スコア18194

answer CHANGED
@@ -27,13 +27,70 @@
27
27
  大抵はこれで何とかなりますが、どうしても `event.currentTarget` が必要と思える状況が出てきた場合は `event.currentTarget` を返す関数を定義して関数呼び出しして下さい。
28
28
 
29
29
  ```JavaScript
30
+ (function () {
31
+
30
- // 「要素ノードを返す関数」を生成する関数
32
+ var getCurrentTarget; // event.currentTarget を返す関数
33
+
31
- function createGetElement (element) {
34
+ function createGetElement (element) { // 「要素ノードを返す関数」を生成する関数
32
- return function getElement () { return element; };
35
+ return function getElement () { return element; };
33
- };
36
+ }
37
+
38
+ function handleClick (event) {
39
+ var currentTarget = event.currentTarget || getCurrentTarget();
40
+ }
41
+
42
+ function initEvent () {
43
+ var target = document.getElementById('sample');
44
+
45
+ getCurrentTarget = createGetElement(target);
46
+
47
+ if (target.addEventLisetner) {
48
+ target.addEventLisetner('click', handleClick, false);
49
+ } else if (target.attachEvent) {
50
+ target.attachEvent('onclick', handleClick);
51
+ }
52
+ }
53
+ }());
34
54
  ```
35
55
 
36
56
  あとは、「`event.currentTarget` をグローバル変数にする」という方法もないわけではありませんが、お勧めしません。
37
57
 
58
+ ---
59
+
38
- 最後に `window` `unload` に全てのイベントハンドラ(イベントリスナ)を `detachEvent` (`removeEventListener`) を実行する事をお勧めします。
60
+ 最後に `window.onunload` 時に全てのイベントハンドラ(イベントリスナ)を `detachEvent` (`removeEventListener`) を実行する事をお勧めします。
39
- この処理を入れておくと循環参照が発生していたとしても `window` `unload` のタイミングで順参照が切れる為、メモリリークしません。
61
+ この処理を入れておくと循環参照が発生していたとしても `window.onunload` のタイミングで順参照が切れる為、メモリリークしません。
62
+
63
+ ```JavaScript
64
+ (function () {
65
+
66
+ function handleClick (event) {
67
+ }
68
+
69
+ function handleDOMContentLoaded (event) {
70
+ }
71
+
72
+ function handleUnload (event) { // 全てのイベント登録を解除する
73
+ if (document.removeEventListener) {
74
+ document.removeEventListener('click', handleClick, false);
75
+ document.removeEventListener('DOMContentLoaded', handleDOMContentLoaded, false);
76
+ removeEventListener('unload', handleUnload, false);
77
+ } else if (document.detachEvent) {
78
+ document.detachEvent('onclick', handleClick);
79
+ detachEvent('onloaded', handleDOMContentLoaded);
80
+ detachEvent('onunload', handleUnload);
81
+ }
82
+ }
83
+
84
+ function initEvent () {
85
+ if (document.addEventListener) {
86
+ document.addEventListener('click', handleClick, false);
87
+ document.addEventListener('DOMContentLoaded', handleDOMContentLoaded, false);
88
+ addEventListener('unload', handleUnload, false);
89
+ } else if (document.attachEvent) {
90
+ document.attachEvent('onclick', handleClick);
91
+ attachEvent('onload', handleDOMContentLoaded);
92
+ attachEvent('onload', handleUnload);
93
+ }
94
+ }
95
+ }());
96
+ ```