Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Possible duplicate of stackoverflow.com/questions/901115/… –  vidya sagar Mar 3 at 5:49
add comment

3 Answers

up vote 1 down vote accepted

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()
share|improve this answer
add comment

Use this :

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

Running it on this page yields : 22139563#22139563

share|improve this answer
    
Man you're fast. –  drewish Mar 3 at 5:51
    
@drewish (My pc has SSD drive...) :-) –  Royi Namir Mar 3 at 5:51
    
Voting you up because you beat me on the pop() ;) –  drewish Mar 3 at 5:52
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.