-9

ハッシュがない場合、location.hash.match は何を返すか教えてください。

私のコード:

function getHashValue(key) {
    return location.hash.match(new RegExp(key + '=([^&]*)'))[1];
}
test = getHashValue('test');

if (test == 'abc') {
    //code WORKS
}   
else if (test == 'sal') {
    //code WORKS
}     
else if (test == "") {
    //code DOESNT WORKS
}

しかし、それは動作しません

私のコード「getHashValue」がハッシュの値を返すことを忘れていました例: #test=abc

すいません言い忘れました

4

3 に答える 3

2

なぜだけではないのですか?

test = getHashValue('test');
if (test === undefined) {
  //code
}

編集

エラーは、match()呼び出しで null が返されたことによるものです。次の変更は、一致が "" または null の場合、空の文字列を返します。

function getHashValue(key) {
    var match = location.hash  .match(new RegExp(key + '=([^&]*)'));
    return match ? match[1] : "";
}
于 2013-07-30T16:09:38.450 に答える