Attack Work TMBeta
Productivity tools to accomplish work faster
 
Username Password
   
Password help
Create new account
Skip Navigation Links.

Javascript - Parse querystring and similar text

Steal This Site [Web Developer Blog]


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&#91;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);
}

Subscribe in a reader
Michael Khalili | Thursday, September 18, 2008 | Updated on Thursday, September 25, 2008
Comments
All guest comments are moderated.
Be the first to comment on this post
Post your comment
Guest Name* !
Email Address* !(Not published)
Website !
Comment*
!
*Required fields


Terms of Service | Privacy Policy | Steal This Site
Attack Work and Voxback are trademarks of Hillside Holdings.