3

window.location.href裁判官はどうやって作るの?

window.location.href一致しない場合?search=、現在の URL をジャンプ先にするhttp://localhost/search?search=car

私のコードは機能しませんか、それともindexOf判断するために使用する必要がありますか? ありがとう。

if(!window.location.href.match('?search='){
    window.location.href = 'http://localhost/search?search=car';
}
4

1 に答える 1

8

いくつかのこと: 閉じ括弧がありません。? をエスケープする必要があります。正規表現にとって重要だからです。またはのいずれ/\?search=/かを使用します'\\?search='

// Create a regular expression with a string, so the backslash needs to be
// escaped as well.
if (!window.location.href.match('\\?search=')) {
    window.location.href = 'http://localhost/search?search=car';
} 

また

// Create a regular expression with the /.../ construct, so the backslash
// does not need to be escaped.
if (!window.location.href.match(/\?search=/)) {
    window.location.href = 'http://localhost/search?search=car';
} 
于 2011-11-07T19:17:10.410 に答える