Last time when I was working in a HTML file I was needed to read the query string values and wanted to do some dynamic operations with those, but you know we cannot straight away read the query string values unless we use a server side technology.
So I searched in the Internet got a lot of examples but most of them are used Regular expression. I’m not quite comfortable with Regular Expressions, So I decided to find a simple script which anybody can easily understand it.
Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function getQS(qParam) { //get all query strings from url var allQS = window.location.search.substring(1); //split the query strings by & var splitQS = allQS.split('&'); //loop through all strings for (var i = 0; i < splitQS.length; i++) { var singleQS = splitQS[i].split('='); //check the single QS and the param passed is matched, if true then return if (singleQS[0] == qParam) { return singleQS[1]; } } } |
Usage Example:
1 |
alert(getQS('your_query_param')); |
The above code is self explanatory, you can easily understand if you have good knowledge in JavaScript.
And here is the demo: