5

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?

Duk
  • 905
  • 5
  • 15
  • 34
  • Possible duplicate of https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript?rq=1 – vidya sagar Mar 03 '14 at 05:49

5 Answers5

5

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

Use this :

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

Running it on this page yields : 22139563#22139563

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
2

Use this code:

var id = location.split('/').pop();
Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
Jarvis Stark
  • 611
  • 5
  • 11
1

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

drewish
  • 9,042
  • 9
  • 38
  • 51
1

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

var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);
myfunkyside
  • 3,890
  • 1
  • 17
  • 32