javascript中的location.search是什么

javascript中的location.search是什么,javascript,Javascript,我想知道location.search.substring(1)实际上做什么。我在一些网站上看到了这个代码。我尝试使用警报打印,但没有给出任何结果。它应该提醒位置href吗 alert(location.search.substring(1)) search属性返回URL的查询部分,包括问号(?) 这意味着location.search.substring(1)应该返回不带问号的数据 // http://www.example.com/index.html console.log(locati

我想知道
location.search.substring(1)
实际上做什么。我在一些网站上看到了这个代码。我尝试使用
警报打印,但没有给出任何结果。它应该提醒位置href吗

alert(location.search.substring(1))
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"

“查询porpotion”是查询字符串:

http://www.example.com/?property=value&property2=value
                       |        query string         |

因此,该代码将返回整个查询参数,不带问号。

它返回查询字符串,不带初始问号。只有当页面上有查询字符串时,才会看到结果,例如,..

location.search属性包含URI(包括?)的查询字符串(如果有)

// 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"
例如:

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

所以你的代码剪掉了前导?并返回
param=arg

,例如,如果您有以下url

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

然后
window.location.search
将返回
?Browser=Netscape
作为字符串

现在是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); 

location.search返回包含初始问号的查询字符串。substr方法是从返回的查询字符串中删除初始问号

在alert()中没有得到任何结果的原因是,您试图在没有任何结果的网站上读取查询字符串

以下是您的测试方法:

  • 去这个
  • 打开浏览器上的控制台
  • 粘贴下面共享的代码段,然后按enter键
  • var容器={};
    location.search.split('&').toString().substr(1).split(“,”).forEach(项=>{
    容器[item.split(“=”[0]]=decodeURIComponent(item.split(“=”[1])?item.split(“=”[1]”):“没有可用的查询字符串”;
    });
    
    控制台日志(容器)
    您能描述一下href上的查询部分是什么,以及它看起来如何您的第一句话与您的示例不匹配-location.search包括?。我不知道它是不正确的,这就是我发布问题的原因here@jchand你的问题没有错——蒂姆的答案是错的。我的另一个评论是对他的回答的回应。我试图像提醒(window.location.search)一样提醒它;我正在url中写入一些查询字符串,然后单击以查看警报,但它没有显示任何内容。警报弹出,但没有显示任何内容。是什么原因造成的?我需要一个你的url和查询的确切例子来重现你的问题。尽管你也可以尝试浏览器的调试模式。在所需行设置断点,只需在控制台中键入
    window.location.search
    window.location
    。搜索返回URL的查询部分,包括问号(?)。这将返回一个字符串,然后我们对该字符串执行子字符串操作。子字符串(1)表示返回跳过第一个字符的字符串。我认为我们的案例是“问号”。