Asked 7 years ago
4 Jan 2017
Views 1027
yogi

yogi posted

get Query string from URL in JavaScript

How to get Query String from URL at JavaScript or any framework like jQuery , Mootools .i want to avoid extra over head of bytes so i want to keep it with JavaScript core . but if easy to do with framework or plugin . i can try it out .
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00


suppose url :: http://localhost/test/showprogress.php?action=testAction&type=json



console.log(window.location.search);// ?action=testAction&type=json
console.log(document.location.search);// ?action=testAction&type=json


search property give the query string for window.location and document.location

but it will not give query string for the SEO friendly URL
http://localhost/test/showprogress/testAction/json
window.location.search and document.location.search will return ""(empty)

but if have URL like ::

http://localhost/test/showprogress/testAction?json



console.log(window.location.search);// ?json
console.log(document.location.search);// ?json


window.location.search and document.location.search return part of URL after "?"
for URL : http://localhost/test/showprogress/testAction?type=json , window.location.search will return ?type=json
hope it clear
Post Answer