0

私は何かをしたい:何かが次のようなURLアドレスを入力した場合:(www.mydomain.com/searchリクエスト部分なし)、javascriptで何かをしたい、のようなURLをwww.mydomain.com/search?search=car入力しますが、私のコードでは何も起こりませんでした。

   <script>
    var curent_url = document.URL;
    if(document.URL.indexOf("?") < 0){ //if(!document.URL.match(/\?/)){
        //alert('ok');
        document.URL = curent_url+'?search=car';
    }
    </script>
4

1 に答える 1

2

の代わりにdocument.URL、空の を確認しますdocument.location.search

if (document.location.search.length === 0) {
  // empty query string... append your part to doc
  document.location.href = document.location.href + "?search=car";
}

コンソールでオブジェクトを調べdocument.locationて、他に何が提供されているかを確認します。通常、解析する必要もdocument.URLdocument.location.href直接解析する必要もありません。

console.dir(document.location);
于 2012-05-25T13:53:56.217 に答える