i have an URL like the followin,
http://test.com/testing/test/12345
12345是id。我想使用查询字符串获取它。如何在JavaScript中获取这个值?
i have an URL like the followin,
http://test.com/testing/test/12345
12345是id。我想使用查询字符串获取它。如何在JavaScript中获取这个值?
试试这样做
http://test.com/testing/test/12345
var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];
var id = window.location.href.split('/').pop()
使用以下代码:
document.location.href.split('/').pop()
在此页面上运行它将得到:22139563#22139563
var id = location.split('/').pop();
我会使用substring
,因为我认为它比创建一个数组更轻量级:
var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);
window.location
访问页面的URL。window.location.pathname
中获得,可以使用正斜杠拆分:window.location.pathname.split('/')
然后,您可以获取数组的最后一个项目:window.location.pathname.split('/').pop()