如何从javascript检索GET参数? [重复]

本文翻译自:How to retrieve GET parameters from javascript? [duplicate]

This question already has answers here : 这个问题已经在这里有了答案
Closed 2 years ago . 2年前关闭。
http://domain.com/page.html?returnurl=%2Fadmin

For js within page.html ,how can it retrieve GET parameters? 对于page.html js ,如何获取GET参数?

For the above simple example, func('returnurl') should be /admin 对于上面的简单示例, func('returnurl')应该为/admin

But it should also work for complex querystrngs... 但是它也应该适用于复杂的querystrngs ...


#1楼

参考:https://stackoom.com/question/MrPl/如何从javascript检索GET参数-重复


#2楼

a more fancy way to do it: :) 一种更花哨的方法::)

var options = window.location.search.slice(1)
                      .split('&')
                      .reduce(function _reduce (/*Object*/ a, /*String*/ b) {
                        b = b.split('=');
                        a[b[0]] = decodeURIComponent(b[1]);
                        return a;
                      }, {});

#3楼

tl;dr solution on a single line of code using vanilla javascript tl; dr使用香草javascript的单行代码解决方案

var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})

This is the simplest solution . 这是最简单的解决方案 It unfortunately does not handle multi-valued keys and encoded characters. 不幸的是,它不处理多值键和编码字符。

"?a=1&a=%2Fadmin&b=2&c=3&d&e"
> queryDict
a: "%2Fadmin"  //overriden with last value, not decoded.
b: "2"
c: "3"
d: undefined
e: undefined

multi-valued keys and encoded characters ? 多值键编码字符

See the original answer at How can I get query string values in JavaScript? 请参阅如何在JavaScript中获取查询字符串值的原始答案

"?a=1&b=2&c=3&d&e&a=5&a=t%20e%20x%20t&e=http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dståle%26car%3Dsaab&a=%2Fadmin"
> queryDict
a: ["1", "5", "t e x t", "/admin"]
b: ["2"]
c: ["3"]
d: [undefined]
e: [undefined, "http://w3schools.com/my test.asp?name=ståle&car=saab"]


In your example, you would access the value like this: 在您的示例中,您将访问以下值:

 "?returnurl=%2Fadmin" > qd.returnurl // ["/admin"] > qd['returnurl'] // ["/admin"] > qd.returnurl[0] // "/admin" 

#4楼

My solution expands on @tak3r 's 我的解决方案扩展到@ tak3r的

It returns an empty object when there are no query params and supports the array notation ?a=1&a=2&a=3 : 当没有查询参数时,它返回一个空对象,并支持数组符号?a=1&a=2&a=3

function getQueryParams () {
  function identity (e) { return e; }
  function toKeyValue (params, param) {
    var keyValue = param.split('=');
    var key = keyValue[0], value = keyValue[1];

    params[key] = params[key]?[value].concat(params[key]):value;
    return params;
  }
  return decodeURIComponent(window.location.search).
    replace(/^\?/, '').split('&').
    filter(identity).
    reduce(toKeyValue, {});
}

#5楼

我这样做(要检索特定的get-parameter,在这里是“ parameterName”):

var parameterValue = decodeURIComponent(window.location.search.match(/(\?|&)parameterName\=([^&]*)/)[2]);

#6楼

如果您不介意使用库而不是滚动自己的实现,请查看https://github.com/jgallen23/querystring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值