javascript 如何使用javascript获取查询字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22139527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:32:06  来源:igfitidea点击:

how to get query string value using javascript

javascriptquery-string

提问by Duk

i have an URL like the followin,

我有一个像下面这样的网址,

http://test.com/testing/test/12345

12345 is the id. I want to take this using query string. How to take this value in javascript?

12345 是 ID。我想使用查询字符串来获取它。如何在javascript中取这个值?

回答by Vinay Pratap Singh

try like this

像这样尝试

http://test.com/testing/test/12345

var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];

or just

要不就

 var id = window.location.href.split('/').pop()

回答by Royi Namir

Use this :

用这个 :

document.location.href.split('/').pop()

document.location.href.split('/').pop()

Running it on this page yields : 22139563#22139563

在此页面上运行它会产生: 22139563#22139563

回答by Jarvis Stark

Use this code:

使用此代码:

var id = location.split('/').pop();

回答by drewish

That's part of the path, not the query string... but you can access the page's URL using window.location.

这是路径的一部分,而不是查询字符串...但您可以使用 window.location.

The path is available at window.location.pathnamewhich can be split up using forward slashes: window.location.pathname.split('/')

window.location.pathname可以使用正斜杠分割的路径可用:window.location.pathname.split('/')

And then you can get the last item of the array: window.location.pathname.split('/').pop()

然后你可以得到数组的最后一项: window.location.pathname.split('/').pop()

回答by myfunkyside

I would use substring, I think it's lighter than creating an array:

我会使用substring,我认为它比创建数组更轻:

var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);