回答編集履歴

4

event-typeの修正(onload→onunload)

2015/08/25 02:26

投稿

think49
think49

スコア18162

test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
  document.detachEvent('onclick', handleClick);
160
160
 
161
- detachEvent('onloaded', handleDOMContentLoaded);
161
+ detachEvent('onload', handleDOMContentLoaded);
162
162
 
163
163
  detachEvent('onunload', handleUnload);
164
164
 
@@ -184,7 +184,7 @@
184
184
 
185
185
  attachEvent('onload', handleDOMContentLoaded);
186
186
 
187
- attachEvent('onload', handleUnload);
187
+ attachEvent('onunload', handleUnload);
188
188
 
189
189
  }
190
190
 

3

initEvent(); の実行漏れ修正

2015/08/25 02:26

投稿

think49
think49

スコア18162

test CHANGED
@@ -102,6 +102,10 @@
102
102
 
103
103
  }
104
104
 
105
+
106
+
107
+ initEvent();
108
+
105
109
  }());
106
110
 
107
111
  ```
@@ -186,6 +190,10 @@
186
190
 
187
191
  }
188
192
 
193
+
194
+
195
+ initEvent();
196
+
189
197
  }());
190
198
 
191
199
  ```

2

変数名の修正(target→currentTarget)

2015/08/25 01:12

投稿

think49
think49

スコア18162

test CHANGED
@@ -82,21 +82,21 @@
82
82
 
83
83
  function initEvent () {
84
84
 
85
- var target = document.getElementById('sample');
85
+ var currentTarget = document.getElementById('sample');
86
86
 
87
87
 
88
88
 
89
- getCurrentTarget = createGetElement(target);
89
+ getCurrentTarget = createGetElement(currentTarget);
90
90
 
91
91
 
92
92
 
93
- if (target.addEventLisetner) {
93
+ if (currentTarget.addEventLisetner) {
94
94
 
95
- target.addEventLisetner('click', handleClick, false);
95
+ currentTarget.addEventLisetner('click', handleClick, false);
96
96
 
97
- } else if (target.attachEvent) {
97
+ } else if (currentTarget.attachEvent) {
98
98
 
99
- target.attachEvent('onclick', handleClick);
99
+ currentTarget.attachEvent('onclick', handleClick);
100
100
 
101
101
  }
102
102
 

1

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

2015/08/25 00:53

投稿

think49
think49

スコア18162

test CHANGED
@@ -56,13 +56,53 @@
56
56
 
57
57
  ```JavaScript
58
58
 
59
- // 「要素ノードを返す関数」を生成する関数
59
+ (function () {
60
60
 
61
- function createGetElement (element) {
62
61
 
63
- return function getElement () { return element; };
64
62
 
63
+ var getCurrentTarget; // event.currentTarget を返す関数
64
+
65
+
66
+
67
+ function createGetElement (element) { // 「要素ノードを返す関数」を生成する関数
68
+
69
+ return function getElement () { return element; };
70
+
65
- };
71
+ }
72
+
73
+
74
+
75
+ function handleClick (event) {
76
+
77
+ var currentTarget = event.currentTarget || getCurrentTarget();
78
+
79
+ }
80
+
81
+
82
+
83
+ function initEvent () {
84
+
85
+ var target = document.getElementById('sample');
86
+
87
+
88
+
89
+ getCurrentTarget = createGetElement(target);
90
+
91
+
92
+
93
+ if (target.addEventLisetner) {
94
+
95
+ target.addEventLisetner('click', handleClick, false);
96
+
97
+ } else if (target.attachEvent) {
98
+
99
+ target.attachEvent('onclick', handleClick);
100
+
101
+ }
102
+
103
+ }
104
+
105
+ }());
66
106
 
67
107
  ```
68
108
 
@@ -72,6 +112,80 @@
72
112
 
73
113
 
74
114
 
75
- 最後に `window` `unload` 時「に全てのイベントハンドラ(イベントリスナ)」を `detachEvent` (`removeEventListener`) を実行する事をお勧めします。
115
+ ---
76
116
 
117
+
118
+
119
+ 最後に `window.onunload` 時に全てのイベントハンドラ(イベントリスナ)を `detachEvent` (`removeEventListener`) を実行する事をお勧めします。
120
+
77
- この処理を入れておくと循環参照が発生していたとしても `window` `unload` のタイミングで順参照が切れる為、メモリリークしません。
121
+ この処理を入れておくと循環参照が発生していたとしても `window.onunload` のタイミングで順参照が切れる為、メモリリークしません。
122
+
123
+
124
+
125
+ ```JavaScript
126
+
127
+ (function () {
128
+
129
+
130
+
131
+ function handleClick (event) {
132
+
133
+ }
134
+
135
+
136
+
137
+ function handleDOMContentLoaded (event) {
138
+
139
+ }
140
+
141
+
142
+
143
+ function handleUnload (event) { // 全てのイベント登録を解除する
144
+
145
+ if (document.removeEventListener) {
146
+
147
+ document.removeEventListener('click', handleClick, false);
148
+
149
+ document.removeEventListener('DOMContentLoaded', handleDOMContentLoaded, false);
150
+
151
+ removeEventListener('unload', handleUnload, false);
152
+
153
+ } else if (document.detachEvent) {
154
+
155
+ document.detachEvent('onclick', handleClick);
156
+
157
+ detachEvent('onloaded', handleDOMContentLoaded);
158
+
159
+ detachEvent('onunload', handleUnload);
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ function initEvent () {
168
+
169
+ if (document.addEventListener) {
170
+
171
+ document.addEventListener('click', handleClick, false);
172
+
173
+ document.addEventListener('DOMContentLoaded', handleDOMContentLoaded, false);
174
+
175
+ addEventListener('unload', handleUnload, false);
176
+
177
+ } else if (document.attachEvent) {
178
+
179
+ document.attachEvent('onclick', handleClick);
180
+
181
+ attachEvent('onload', handleDOMContentLoaded);
182
+
183
+ attachEvent('onload', handleUnload);
184
+
185
+ }
186
+
187
+ }
188
+
189
+ }());
190
+
191
+ ```