how to get query string value using javascript

给你一囗甜甜゛ 提交于 2019-12-23 14:47:08

问题


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?


回答1:


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()



回答2:


Use this :

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

Running it on this page yields : 22139563#22139563




回答3:


Use this code:

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



回答4:


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

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

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




回答5:


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

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


来源:https://stackoverflow.com/questions/22139527/how-to-get-query-string-value-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!