49

location.search.substring(1)私は実際に何をするのか知りたいです。このコードは、いくつかの Web サイトで見ました。を使用して印刷しようとしましalertたが、結果は得られませんでした。ロケーションhrefに警告することになっていますか?

alert(location.search.substring(1))
4

7 に答える 7

52
http://example.com/index.php?foo=bar

location.search
> ?foo=bar
location.search.substring(1)
> foo=bar

そのため、コードは疑問符なしでクエリパラメータ全体を返します。

于 2013-01-18T08:38:58.443 に答える
21

location.searchプロパティには、URIのクエリ文字列(?を含む)が含まれています(存在する場合)。

例:

http://www.example.org/index.php?param=arg
location.search is ?param=arg

それで、あなたのコードは先頭を切り取っていますか?を返しますparam=arg

于 2013-01-18T08:39:35.883 に答える
14

searchプロパティは、疑問符(?)を含むURLのクエリ部分を返します。

つまり、疑問符なしlocation.search.substring(1)でデータを返す必要があります。

// http://www.example.com/index.html
console.log(location.search.substring(1)); // no query string, so displays nothing

// http://www.example.com/index.html?property=value
console.log(location.search.substring(1)); // should display "property=value"

「queryporpotion」はクエリ文字列です。

http://www.example.com/?property=value&property2=value
                       |        query string         |
于 2013-01-18T08:38:32.420 に答える
7

たとえば、次のURLがある場合

http://www.example.org/index.htm?Browser=Netscape

その後、文字列としてwindow.location.search返されます?Browser=Netscape

于 2013-01-18T08:41:15.807 に答える
1

最初の疑問符なしでクエリ文字列を返します。ページにクエリ文字列がある場合にのみ結果が表示されます(例:http ://www.example.com?parameter = value) 。

于 2013-01-18T08:39:06.903 に答える
1

今は 2018 年ですが、これが 2018 年のやり方です。

サンプル URL:

http://localhost:10/mapserver1/viewer/?config=viewer_simple1&url=https://maps2.dcgis.dc.gov/dcgis/rest/services/Zoning/MapServer&zoom=17&lat=38.917292&long=-77.036420

各クエリパラメータを抽出する私の作業コード:

        var ___zoom;
        var ___lat;
        var ___long;
        var ___basemap;

        var ___type;
        var ___url;
        var ___title;
        var ___opacity;



        if ( location.search.match(/zoom=([^&]*)/i) )
        {
             ___zoom = location.search.match(/zoom=([^&]*)/i)[1];
         }

        if ( location.search.match(/lat=([^&]*)/i) )
        {
           ___lat = location.search.match(/lat=([^&]*)/i)[1];
        }

        if (location.search.match(/long=([^&]*)/i))
        {
            ___long = location.search.match(/long=([^&]*)/i)[1];
        }

        if (location.search.match(/basemap=([^&]*)/i))
        {
            ___basemap = location.search.match(/basemap=([^&]*)/i)[1];
        }

        if (location.search.match(/type=([^&]*)/i))
        {
            ___type = location.search.match(/type=([^&]*)/i)[1];
        }

       if (location.search.match(/url=([^&]*)/i))
        {
            ___url = location.search.match(/url=([^&]*)/i)[1];
        }


        if (location.search.match(/title=([^&]*)/i))
        {
            ___title = location.search.match(/title=([^&]*)/i)[1];
        }

        if (location.search.match(/opacity=([^&]*)/i))
        {
            ___opacity = location.search.match(/opacity=([^&]*)/i)[1];
        }


        //console.log(location.search.match(/zoom=([^&]*)/i)[0]);   //    'zoom=17'
        //console.log(location.search.match(/zoom=([^&]*)/i)[1]);   //     '17'
        console.log(___zoom); 
        console.log(___lat); 
        console.log(___long); 
        console.log(___basemap); 

        console.log(___type); 
        console.log(___url); 
        console.log(___title); 
        console.log(___opacity); 
于 2018-09-11T22:46:17.660 に答える