There is no built in function in Javascript to parse the querystring of a URL. That means a querystring parser is a basic function you need to add for Javascript development. What I did with mine was create it in such a way that allowed me to use it for all strings that have similar formatting. Now when I want to pass data back to the client, via Ajax or other method, I could do so using a string with the FieldName1=FieldValue1&FieldName2=FieldValue2 format.
Code:function QueryStringLikeParse(strQuery, strFieldName) {
strQuery = String(strQuery).toLowerCase();
var aryPairs = strQuery.split("&");
var aryPair;
strFieldName = strFieldName.toLowerCase();
for (i=0;i<aryPairs.length;i++) {
aryPair = aryPairs[i].split("=");
if (aryPair[0] == strFieldName){
return aryPair[1];
}
}
return '';
}
strQuery is the string you want parsed.
strFieldName is the field name you want the field value of.
To get a value from the querystring use window.location.search.substring(1) for the strQuery value. The two code blocks below show how to get the value for a querystring field called MyFieldName.
Code:QueryStringLikeParse(window.location.search.substring(1), 'MyFieldName');
Code:document.write(QueryStringRequest('MyFieldName'));
function QueryStringRequest(strFieldName) {
return QueryStringLikeParse(window.location.search.substring(1), strFieldName);
}