Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get query string value using javascript

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?

like image 837
Duk Avatar asked Mar 03 '14 05:03

Duk


People also ask

How do I get query string values in Javascript?

getAll() : This is to get an array of all the values in the query string. forEach() : This is to iterate over the parameters of the query string. keys() : This is to iterate over the keys of the query string. values() : This is to iterate over the values of the query string.

What is query string in Javascript?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.

Can you use Javascript to get URL parameter values?

The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.

What is query params in Javascript?

Query parameters are those funny strings in a URL after the question mark ( ? ). Here's an example of a URL with a query parameter: https://www.


2 Answers

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()
like image 106
Vinay Pratap Singh Bhadauria Avatar answered Nov 01 '22 11:11

Vinay Pratap Singh Bhadauria


Use this :

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

Running it on this page yields : 22139563#22139563

like image 33
Royi Namir Avatar answered Nov 01 '22 09:11

Royi Namir