JavaScript メモ

配列

配列のコピーは concat でOK
var arr1 = [1,2,3];
var arr2 = arr1.concat();

http://d.hatena.ne.jp/uupaa/20100116/1263640217

IE filter

filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#B4000000, endcolorstr=#B4000000, gradienttype=0);

color の最初の2桁(16進)が透過度を示す

  • IE7 では filter を使った場合、clearType のフォント(メイリオとか)が細くなる(アンチエイリアスがなくなる)という問題あり
  • filter は CPU に負荷をかけるので、あまり多用しないほうがいい

http://css-eblog.com/csstechnique/rgba-for-ie.html

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);

event

非標準機能
<body oncontextmenu="return false">

で右クリックのコンテキストメニューを禁止。IEFirefoxのみ対応。