how to get HTTP GET request value using javascript

Possible Duplicate:
How can I get query string values?

how can I get the HTTP GET request using javascript?

for example if I have access www.sample.com/div/a/dev.php?name=sample

how can I get the GET request of name=sample and the value if name which is sample ?


window.location对象可能在这里很有用:

var parameter = window.location.search.replace( "?", "" ); // will return the GET parameter 

var values = parameter.split("=");

console.log(values); // will return and array as ["name", "sample"] 

Here is a fast way to get an object similar to the PHP $_GET array:

function get_query(){
    var url = location.href;
    var qs = url.substring(url.indexOf('?') + 1).split('&');
    for(var i = 0, result = {}; i < qs.length; i++){
        qs[i] = qs[i].split('=');
        result[qs[i][0]] = qs[i][1];
    }
    return result;
}
Usage:

var $_GET = get_query();
For the query string x=5&y&z=hello&x=6 this returns the object:

{
  x: "6",
  y: undefined,
  z: "hello"
}

您可以使用location.href来获取完整的URL,然后使用split提取值

链接地址: http://www.djcxy.com/p/17614.html

上一篇: 我怎样才能通过javascript读取url的GET元素?

下一篇: 如何使用javascript获取HTTP GET请求值