JavaScript メモ
配列
配列のコピーは concat でOK
var arr1 = [1,2,3]; var arr2 = arr1.concat();
IE filter
filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#B4000000, endcolorstr=#B4000000, gradienttype=0);
color の最初の2桁(16進)が透過度を示す
CSS 参照
list-style
//css .hoge { list-style: url(xxx) } //js var hoge = document.getElement... var cs = cs = _ie ? div.currentStyle : window.getComputedStyle(hoge, null); cs.listStyleImage // url(xxx)
ブラウザ判定
IEか否か
var isIE = !!document.uniqueID;
isIE = !jQuery.support.opacity,
isIE6 = isIE && !window.XMLHttpRequest
数値変換
parseFloat
Firefox2 - jQuery.browser.version // 1.8.1.17 Firefox3.5 - jQuery.browser.version // 1.9.1.12 var is_oldFx = parseInit(jQuery.browser.version)/*1.8*/ < 1.9;
URL変換
encodeURIComponent / decodeURIComponent
href や location の設定、参照に
alert(encodeURIComponent("あ")); // %E3%81%82 alert(decodeURIComponent("%E3%81%82")); // あ
Function
関数の定義処理タイミング
関数宣言はコードの実行より先に行われる
f.cnt = 100; function f(){ f.cnt++; return f.cnt; } alert(f()); //101
apply と call
callの場合、第2引数が配列でない
var f = function(){...} f.call(this,1,2,3); f.apply(this,[1,2,3]);
引数の定義数の取得
alert(function(a,b,c){}.length); //3
自function を取得
var f; (f = function(){ alert(arguments.callee == f); // true })();
arguments の参照
(function (x) { x = 2; alert(arguments[0]); //2が表示される!! http://subtech.g.hatena.ne.jp/cho45/20100221/1266680746 })(1);