querystring 처리 클래스

12496 단어 String

  
    
/*
* querystring( ?userid=222&username=23432sdf)
*/
function PageQuery(q) {
if (q.length > 1 )
this .q = q.substring( 1 , q.length);
else
this .q = null ;
//
this .keyValuePairs = new Array();
if ( this .q) {
var infos = this .q.split( " & " );
for ( var i = 0 ; i < infos.length; i ++ ) {
//
this .keyValuePairs.push(infos[i]);
}
}
this .getKeyValuePairs = function () { return this .keyValuePairs };

//
this .getLength = function () { return this .keyValuePairs.length };

// key, value
this .getValue = function (s) {
var length = this .getLength();
for ( var i = 0 ; i < length; i ++ ) {
if ( this .keyValuePairs[i].split( " = " )[ 0 ] == s)
return this .keyValuePairs[i].split( " = " )[ 1 ];
}
};

//
this .getParameters = function () {
var parameters = new Array();
var length = this .getLength();
for ( var i = 0 ; i < length; i ++ ) {
parameters.push(
this .keyValuePairs[i].split( " = " )[ 0 ]);
}
return parameters;
}
}

function analysisUrl() {
// this.location.search ?
//
alert(this.location.search);
var page = new PageQuery( " ?userid=234&username=sdfsd " );
// alert(page.getLength());
alert(page.getValue( " username " ));
var params = page.getParameters();
for ( var i = 0 ; i < params.length; i ++ ) {
alert(params[i]);
}
}
< / script>
, 。

좋은 웹페이지 즐겨찾기