-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns_javascript.html
403 lines (342 loc) · 11.7 KB
/
patterns_javascript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="ja" />
<title>JAVASCRIPT TEMPLATE MODULES</title>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<style type="text/css">
* {
margin:0;
padding:0;
font-size:100%;
border:0px;
}
body{
font-size:12px;
line-height:1.6;
}
#container {
width:960px;
margin:20px auto;
}
h1 {
font-size:2em;
margin:0.5em 0;
border-bottom:2px solid #CCC;
}
h2 {
font-size:1.35em;
margin:1em 0;
padding:0.2em 0.5em;
font-weight:bold;
color:#336;
background:#f0f0f3;
}
pre {
display:block;
color:#396;
}
pre:before {
content:"/*";
display:block;
}
pre:after {
content:"*/";
display:block;
}
textarea.example {
display:block;
width:920px;
min-height:200px;
padding:15px;
border:5px solid #CCC;
}
hr{
margin:100px 0;
}
.pending {
background:#F00;
color:#FFF;
}
.pending:before {
content:"[要確認]";
}
</style>
</head>
<body>
<div id="container">
<h1>JAVASCRIPT TEMPLATE MODULES</h1>
<!--Template
<h2>Module_Name</h2>
<pre>
</pre>
<textarea class="example">
</textarea>
-->
<div id="sub">
<ul>
<li><a href="#comment">Comment</h2>
<li><a href="#basicOnHTML">HTMLのScript要素に記述する書き方</a></li>
<li><a href="#basic">.jsの基本パターン</a></li>
<li><a href="#browser">ブラウザ判定パターン</a></li>
<li><a href="#debugModeCheck">デバッグモードチェック・パターン</a></li>
<li><a href="#debug">デバッグ・パターン</a></li>
<li><a href="#mecaJs">meca.jsの設定</a></li>
<li><a href="#onload">onload</a></li>
<li><a href="#routing">Routingパターン</a></li>
<li><a href="#swfobject">SWFObject Pattern</a></li>
<li><a href="#windowSize">window size パターン</a></li>
</ul>
</div>
<div id="main">
<h2 id="comment">Comment</h2>
<pre>
・インラインコメント
・ブロックコメント
</pre>
<textarea class="example">// インラインコメント
/*
ブロックコメント
*/</textarea>
<h2 id="basicOnHTML">HTMLのScript要素に記述する書き方</h2>
<pre>
LAB.jsを使用したパターン。
ただし、ノンブロッキング処理のためHTMLのレンダリングとJSによる表示でタイミングがずれるので、
デフォルトでCSS非表示にしてJSで表示とか工夫する必要がある。
</pre>
<textarea class="example">$LAB.script("/js/lib/jquery.js").wait()
.script(["/js/lib/meca.js", "/js/common.js"]).wait()
.script("/js/コンテンツ名.js");</textarea>
<h2 id="basic">.jsの基本パターン</h2>
<pre>
即時関数にグローバルオブジェクトを渡すパターン。
</pre>
<textarea class="example">(function(global){
// initialize...
})(this);</textarea>
<h2 id="browser">ブラウザ判定パターン</h2>
<pre>
jQuery.browserを使う場合(jQuery1.3以降非推奨:<a href="http://api.jquery.com/jQuery.browser/">http://api.jquery.com/jQuery.browser/</a>)
uupaa.jsを使う場合(<a href="http://code.google.com/p/uupaa-js/wiki/uu_env">http://code.google.com/p/uupaa-js/wiki/uu_env</a>)
<span class="pending">自前でやる場合</span>
</pre>
<textarea class="example">//jQuery.browser
jQuery.browser.webkit (as of jQuery 1.4)
jQuery.browser.safari (deprecated)
jQuery.browser.opera
jQuery.browser.msie
jQuery.browser.mozilla
//uupaa.js
uu.env.ie // Boolean: Internet Explorer で true になります。
uu.env.ie6 // Boolean: Internet Explorer 6 で true になります。
uu.env.ie7 // Boolean: Internet Explorer 7 で true になります。
uu.env.ie8 // Boolean: Internet Explorer 8 で document.documentMode が 8 なら true になります。IE5,IE7互換表示モードでは false になります。
uu.env.ie9 // Boolean: Internet Explorer 9 で document.documentMode が 9 なら true になります。IE5,IE7,IE8互換表示モードでは false になります。
uu.env.ie678 // Boolean: Internet Explorer 6~8 true になります。
uu.env.opera // Boolean: Opera, Opera Mini で true になります。
uu.env.gecko // Boolean: Gecko ベースのブラウザ(Firefox等)で true になります。
uu.env.webkit // Boolean: WebKit ベースのブラウザ(Safari, iPhone, iPad, iPod, Android, Google Chrome等)で true になります。
uu.env.chrome // Boolean: Google Chrome で true になります。
uu.env.safari // Boolean: Safari, iPhone, iPad, iPod, Android で true になります。
uu.env.longedge // Number: 画面の長辺を数値で返します。1024 x 600 のデバイスでは 1024 を返します。
uu.env.ipad // Boolean: iPad で true になります。
uu.env.iphone // Boolean: iPhone / iPod で true になります。
uu.env.ios // Boolean: iPad / iPhone / iPod で true になります。
uu.env.retina // Boolean: Retina Display で true になります。iPhone 4 以上の判別にも利用できます。
uu.env.android // Boolean: Android で true になります。
uu.env.mbosver // Number: モバイルOSのバージョンを数値(major.minor)で返します。iOS 3.1.3 は mbosver = 3.13 になります。該当しないデバイスでは 0 を返します。
uu.env.slate // Boolean: モバイルデバイスで石版状の大型端末なら true になります。iPad なら true になり、Androidで長辺が961ピクセル以上の場合も true になります。
uu.env.mobile // Boolean: iOS, Android, Opera Mini で true になります。Windows Phone, Mobile Firefox(Fennec) では現在のところ false になります。
uu.env.touch // Boolean: iOS, Android で true になります。
//自前でやる場合</textarea>
<h2 id="debugModeCheck">デバッグモードチェック・パターン</h2>
<pre>
</pre>
<textarea class="example">var DomainName = global.DomainName || (global.DomainName = {});
DomainName.env = location.host.indexOf('example.com') >= 0 ? 'prod' : 'dev';
</textarea>
<h2 id="debug">デバッグ・パターン</h2>
<pre>consoleが無い環境を考慮しつつconsoleラップするパターン。jQuery依存</pre>
<textarea class="example">$.browser.ipad = navigator.platform == 'iPad';
if ($.browser.ipad) return;
if (!window.console) window.console = {};
var names = [
"log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"
];
for (var i = 0, n; n = names[i]; i++) {
if (typeof(window.console[n]) != 'function') {
window.console[n] = function() {};
}
}
if (!window.Namespace) Namespace = {};
Namespace.env = location.host.indexOf('example.com') >= 0 ? 'prod' : 'dev';
var debug = {
log: function(label) {
if (Namespace.env == 'prod') return;
var args = Array.prototype.slice.call(arguments, 1);
if (!/String/.test(Object.prototype.toString.call(label))) {
args.unshift(label);
label = 'Namespace DEBUG';
}
if ($.browser.ipad) {
console.log(label+ ': ' + args.join(','));
}
else {
args.unshift("[" + label + "]");
if ($.browser.msie) {
console.log(args);
}
else {
console.log.apply(console, args);
}
}
}
};</textarea>
<h2 id="mecaJs">meca.jsの設定</h2>
<pre>@see <a href="http://webtech-walker.com/meca/">http://webtech-walker.com/meca/</a></pre>
<textarea class="example">$(function () {
// ロールオーバー
$('.btn').meca('hover');
// 外部リンク
$('a.external').meca('external');
// pngfix
$('.pngfix').meca('pngfix');
// pngfix(背景画像)
$('.bgpng').meca('bgpngfix');
// 高さ揃え
$('.heightAlign').meca('heightAlign');
// IE6でposition fixed
$('.fixed').meca('positionFixed');
// スムーズスクロール
$('a[href^="#"]').meca('smoothScroll');
// OSのクラス追加
$('body').meca('addOsClass');
// IEで画像のlabel押せるようにする
$('label img').meca('labelClickable');
// active
$('.hasActive').meca('active');
//placeholder
$('input[placeholder], textarea[placeholder]').meca('placeholder');
});</textarea>
<h2 id="onload">onload</h2>
<pre>
window.onloadつかう
<span class="pending">windowのイベントつかう</span>
jQueryつかう(@see <a href="http://api.jquery.com/ready/">http://api.jquery.com/ready/</a>)
uupaa.jsつかう(@see <a href="http://code.google.com/p/uupaa-js/wiki/uu_ready">http://code.google.com/p/uupaa-js/wiki/uu_ready</a>)
</pre>
<textarea class="example">//window.onloadつかう
window.onload = function () {
// initialize dom
};
//windowのイベントつかう
//jQueryつかう
// 複数呼べばハンドラは複数指定できます
// その1
$(document).ready(function(){
// initialize dom
});
// その2
$().ready(function(){
// initialize dom
});
// その3
$(function(){
// initialize dom
});
//uupaa.jsつかう
// イベントが発火済みか調べる
uu.ready.dom
uu.ready.window
uu.ready.audio
uu.ready.video
uu.ready.canvas
uu.ready.storage
uu.ready.reload
// DOMReady
uu.ready(function(uu) {
// initialize dom
});
// ハンドラは複数指定できます(DOMReady1 → DOMReady2 の順に発火)
uu.ready(function(uu) {
//alert("DOMReady1");
}, function(uu) {
//alert("DOMReady2");
});</textarea>
<h2 id="routing">Routingパターン</h2>
<pre>
</pre>
<textarea class="example">
var dispatcher = global.dispatcher || (global.dispatcher = function dispatcher(path, func) {
dispatcher.path_func = dispatcher.path_func || []
if (func) return dispatcher.path_func.push([path, func]);
for(var i = 0, l = dispatcher.path_func.length; i < l; ++i) {
var func = dispatcher.path_func[i];
var match = path.match(func[0]);
match && func[1](match);
};
});
//dispatcher(location.pathname);</textarea>
<h2 id="swfobject">SWFObject Pattern</h2>
<pre>
<a href="http://mtl.recruit.co.jp/blog/2007/10/swfobject_v20.html">SWFObject v2.0 ドキュメント日本語訳(MTL)</a>
</pre>
<textarea class="example">
var flashvars = {},
params = {
wmode:"transparent"
},
attributes = {
wmode:"transparent"
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes);
</textarea>
<h2 id="windowSize">window size パターン</h2>
<pre><span class="pending">書いてない</span>
</pre>
<textarea class="example">
var wid = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var hei = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
</textarea>
<h2>location解析パターン</h2>
<pre><span class="pending">書いてない</span>
</pre>
<textarea class="example">
</textarea>
<h2>Array拡張パターン</h2>
<pre><span class="pending">書いてない</span>
</pre>
<textarea class="example">
</textarea>
<h2>Number拡張パターン</h2>
<pre><span class="pending">書いてない</span>
</pre>
<textarea class="example">
</textarea>
<h2>String拡張パターン</h2>
<pre><span class="pending">書いてない</span>
</pre>
<textarea class="example">
</textarea>
</div>
<!-- [ SCRIPTS ] -->
<script src="/js/lib/LAB.min.js" type="text/javascript"></script>
<script type="text/javascipt" language="javascript">
// Setting.
// Load scripts.
$LAB.script('/js/lib/jquery.1.6.1.js').wait()
.script(['/js/lib/meca.js'/*,'/js/lib/swfobject.js'*/]).wait()
.script('/js//common.js')
/*
.wait(function() {
})
//*/
;// End of Script
</script>
<noscript><p id="msgNoscript">当サイトは、ブラウザのJavaScript設定を有効にしてご覧ください。</p></noscript>
<!-- / [ SCRIPTS ] -->
</body>
</html>