【问题标题】:How to get parameters from a url? [duplicate]如何从 url 中获取参数? [复制]
【发布时间】:2012-09-17 23:28:50
【问题描述】:

可能重复:
How to get the value from URL Parameter?

我有这个网址:http://example.com/photos/5062d95d-13e8-ceb8-bb35-5891b62a3418/?id=872364876

我有这个 javascript 代码:

if (sssssssssss) {
    window.location = "http://example.com/s/1525";
} else {
    location.replace('http://example.com/id=');
}

我需要从else 部分的GET 参数中填充id 值。 有什么想法吗?

【问题讨论】:

    标签: javascript


    【解决方案1】:

    这是一个如何使用 javascript 从 url 获取变量的示例

    function getUrlVars() {
         var vars = {};
         var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
              vars[key] = value;
         });
         return vars;
    }
    

    来源:http://papermashup.com/read-url-get-variables-withjavascript/

    【讨论】:

    • 我喜欢它,但你应该删除最后的#anchors:/[?&]+([^=&]+)=([^]*)/gi
    【解决方案2】:

    检查此代码

    var start= location.href.indexOf('id=');
    var id= location.href.substring(start+ 3, location.length- 1);
    

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 1970-01-01
      • 2012-03-31
      • 2014-01-12
      • 2013-07-27
      • 1970-01-01
      • 2022-01-18
      • 2019-06-20
      • 2012-07-19
      相关资源
      最近更新 更多