Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: AcklenXPosted on 2003-07-16 at 13:43:45ID: 8937989
<script> ////////// ////////// ////////// ////////// ////////// //*/ /// PARSE QUERY STRING ////////////////////////// ///*/ ////////// ////////// ////////// ////////// ////////// //*/
2%2B2%3D4" );
;
earch ); earch.subs tring( 1, document.location.search.l ength ) );
i ); e[ 0 ] + " " + attribute[ 1 ] + " "; i)
2%2B2%3D4" ) 0, sArrayToConvert.length - 1 ) ////////// ////////// ////////// ////////// ////////// //*/ /// PARSE QUERY STRING ////////////////////////// ///*/ ////////// ////////// ////////// ////////// ////////// //*/
ummy?OpenF orm&Type=A " ); ummy?OpenF orm&Type=B " ); ummy?OpenF orm&Blah=1 &La=1&Di=3 & Type=Da" );
/*////////////////////////
/*////////////////////////
/*////////////////////////
function parseQueryString( sQueryString )
/*
The function parseQueryString() has one optional argument,
sQueryString. That string should be in the form of the
"document.location.query" property ( starts with a "?", attribute/values
pairs are "&" delimited, attributes are separated from their
corresponding value by "=", and all special characters ( such as ?, &,
=, etc. ) should be HTTP escaped (i.e. "&" --> "%26" ). If no
argument is passed in the default sQueryString is the
"document.location.query".
This method returns an associative array (it's elements are named, so
don't try to loop with and index, if you need to use a "for in" loop
and be sure to ignore things like "length". The named elements of the
array will be the attributes of the query string and their values will
be the value of that attribute in the query string. All return values
will be http unescaped (and will take care of the "+" to space
conversion if necessary).
ex:
var aQuery = getFormPost( "?name=Quincy&mathProblem=
aQuery.name or aQuery[ "name" ] will return "Quincy";
aQuery.mathProblem will return "2+2=4" (note the escaped "+");
*/
{
getStringToParse();
splitStringToParse();
createNameAndValueArrays()
associateArrays();
return aQueryString;
/* Private Methods - parseQueryString() */
function associateArrays()
{
for ( i = 0; i < aQueryStringNames.length; i++ )
{
if( !aQueryString[ aQueryStringNames[ i ] ] )
aQueryString[ aQueryStringNames[ i ] ] = removePlus( aQueryStringValues[ i ] );
else if( !aQueryString[ aQueryStringNames[ i ] ][ 0 ] )
{
var aNewArray = new Array();
aNewArray.push( aQueryString[ aQueryStringNames[ i ] ] );
aNewArray.push( removePlus( aQueryStringValues[ i ] ) );
aQueryString[ aQueryStringNames[ i ] ] = aNewArray;
}
else // three or more items and the array has already been created
{
debugMe();
aQueryString[ aQueryStringNames[ i ] ].push( removePlus( aQueryStringValues[ i ] ) );
debugMe();
}
function debugMe()
{
return;
alert( aQueryStringNames[ i ] + "\n" +
aQueryString[ aQueryStringNames[ i ] ] + "\n" +
"-->" + removePlus( aQueryStringValues[ i ] ) + "<--" + "\n" +
aQueryString[ aQueryStringNames[ i ] ].length + "\n" +
"-->" + aQueryString[ aQueryStringNames[ i ] ][ aQueryString[ aQueryStringNames[ i ] ].length -1 ] + "<--"
);
var ar = aQueryString[ aQueryStringNames[ i ] ];
var sar = "";
for ( x in ar )
{
sar+= x +":\t" + ar[ x ]+"\n";
}
alert( sar )
}
}
/* Private Methods - associateArrays() */
function removePlus(psEncodeString)
{
var lsRegExp = /\+/g;
return unescape( String( psEncodeString ).replace( lsRegExp, " " ) );
}
/* Private Methods - associateArrays() */
}
function createNameAndValueArrays()
{
aQueryStringNames = new Array();
aQueryStringValues = new Array();
var aQueryString = sDelimitedString.split(" ");
for ( i = 0, a=0; i < aQueryString.length - 1; i += 2, a++ )
{
aQueryStringNames[ a ] = aQueryString[ i ];
aQueryStringValues[ a ] = aQueryString[ i + 1 ];
}
}
function getStringToParse()
{
sQueryString = ( sQueryString || window.document.location.s
var iQueryStart = sQueryString.indexOf( "?" ) + 1;
sQueryString = ( sQueryString.substring( iQueryStart, sQueryString.length ) );
sStrippedString = ( sQueryString || window.document.location.s
}
function splitStringToParse()
{
aQueryString = new Array();
sDelimitedString = '';
var splitString = sStrippedString.split( "&" );
for ( i = 0; i < splitString.length; i++ )
{
removeAmpersandEntityRefs(
var attribute = splitString[ i ].split( "=" );
sDelimitedString+=attribut
}
return sDelimitedString;
/* Private Methods - splitQueryString() */
function removeAmpersandEntityRefs(
{
if(splitString[ i ].indexOf( 'amp;' ) != -1 )
splitString[ i ] = splitString[ i ].substring( 4, splitString[ i ].length );
}
/* Private Methods - splitQueryString() */
}
/* Private Methods - parseQueryString() */
}
function getArrayAsQueryString( aArrayToConvert )
/*
The function getArrayAsQueryString() takes in an associative array
"aArrayToConvert" ( presumably one from "parseQueryString()" ), and
returns a string appropriate for appending to a url ( i.e.
"?name=Quincy&mathProblem=
*/
{
sArrayToConvert = '?';
for ( sAttributes in aArrayToConvert )
{
sArrayToConvert += sAttributes;
sArrayToConvert += '=' + aArrayToConvert[ sAttributes ] + '&';
}
sArrayToConvert = sArrayToConvert.substring(
return sArrayToConvert;
}
/*////////////////////////
/*////////////////////////
/*////////////////////////
var aValue = parseQueryString( "www.domain.com/file.nsf/D
alert( aValue[ "Type" ] )
var aValue = parseQueryString( "www.domain.com/file.nsf/D
alert( aValue.Type )
var aValue = parseQueryString( "www.domain.com/file.nsf/D
alert( aValue.La )
</script>