【问题标题】:What is location.search in javascript什么是 javascript 中的 location.search
【发布时间】:2013-01-18 08:36:16
【问题描述】:

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

alert(location.search.substring(1))

【问题讨论】:

  • location.search 返回 URL 的查询部分,包括问号 (?)。这将返回一个字符串,然后我们对该字符串进行子字符串操作。 substring(1) 表示返回跳过第一个字符的字符串。我的情况是“问号”。

标签: javascript


【解决方案1】:
http://example.com/index.php?foo=bar

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

这样代码将返回整个查询参数没有问号。

【讨论】:

  • 我不知道这是不正确的,这就是我在这里发布问题的原因
  • @jchand 你的问题没有错——蒂姆的回答是不正确的。我的另一条评论是为了回复他的回答。
【解决方案2】:

location.search 返回一个包含初始问号的查询字符串。 substr 方法是从返回的查询字符串中去掉最初的问号。

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

测试方法如下:

  1. 转到此URL
  2. 在浏览器上打开控制台
  3. 粘贴下面分享的代码 sn-p 并回车

var container = {};
location.search.split('&').toString().substr(1).split(",").forEach(item => {
    container[item.split("=")[0]] = decodeURIComponent(item.split("=")[1]) ?  item.split("=")[1]: "No query strings available" ;
});
console.log(container);

老问题,但答案可能对本页的新访问者有所帮助。

【讨论】:

    【解决方案3】:

    它返回查询字符串,不带初始问号。如果页面上有查询字符串,您只会看到结果,例如http://www.example.com?parameter=value.

    【讨论】:

      【解决方案4】:

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

      例子:

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

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

      【讨论】:

      • 您的第一句话与您的示例不匹配 - location.search includes the ?.
      【解决方案5】:

      搜索属性返回 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"
      

      “查询部分”是查询字符串:

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

      【讨论】:

      • 你能描述一下 href 上的查询部分是什么以及它的样子吗
      【解决方案6】:

      例如如果你有以下网址

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

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

      【讨论】:

      • 我正在尝试像 alert(window.location.search);我在 url 中写了一些查询字符串,然后单击查看警报,但它没有显示任何内容。警报弹出但没有显示。是什么原因造成的?
      • 我需要您的网址和查询的确切示例来重现您的问题。虽然你也可以尝试浏览器的调试模式。在您想要的行设置断点,然后在控制台中输入window.location.searchwindow.location。它是一种比警报更好的方法来查看发生了什么。
      【解决方案7】:

      现在是 2018 年,这就是你在 2018 年的做法。

      示例网址:

      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); 
      

      【讨论】:

        猜你喜欢
        • 2020-02-26
        • 2012-02-19
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多