原文:http://www.cftea.com/c/2009/10/BDZ9S07CGGPLPXXH.asp
JS-Code
/**
* get parameters from url
* @method getQuery
* @param {String} key
* @param {String} url
* @return {String | Array}
*/
getQuery : function( key , url )
{
url = url || window.location.href;
var rts = [],rt;
queryReg = new RegExp( '(^|\\?|&)' + key + '=([^&]*)(?=&|#|$)' , 'g' );
while ( ( rt = queryReg.exec( url ) ) != null )
{
rts.push( decodeURIComponent( rt[ 2 ] ) );
}
if ( rts.length == 0 ) return null;
if ( rts.length == 1 ) return rts[ 0 ];
return rts;
},
/**
* set parameters for url
* @method setQuery
* @param {String} key
* @param {String | Array} value
* @param {String} url
* @return {String}
*/
setQuery : function( key , value , url )
{
url = url || window.location.href;
url = url.replace( new RegExp( '(^|\\?|&)' + key + '=[^&]*(?=&|#|$)' , 'g' ) , '' );
value = isArray( value ) ? value : [ value ];
for ( var i = value.length - 1;i >= 0;i --)
{
value[ i ] = encodeURIComponent( value[ i ] );
}
var p = key + '=' + value.join( '&' + key + '=' );
return url + ( /\?/.test( url ) ? '&' : '?' ) + p;
}
* get parameters from url
* @method getQuery
* @param {String} key
* @param {String} url
* @return {String | Array}
*/
getQuery : function( key , url )
{
url = url || window.location.href;
var rts = [],rt;
queryReg = new RegExp( '(^|\\?|&)' + key + '=([^&]*)(?=&|#|$)' , 'g' );
while ( ( rt = queryReg.exec( url ) ) != null )
{
rts.push( decodeURIComponent( rt[ 2 ] ) );
}
if ( rts.length == 0 ) return null;
if ( rts.length == 1 ) return rts[ 0 ];
return rts;
},
/**
* set parameters for url
* @method setQuery
* @param {String} key
* @param {String | Array} value
* @param {String} url
* @return {String}
*/
setQuery : function( key , value , url )
{
url = url || window.location.href;
url = url.replace( new RegExp( '(^|\\?|&)' + key + '=[^&]*(?=&|#|$)' , 'g' ) , '' );
value = isArray( value ) ? value : [ value ];
for ( var i = value.length - 1;i >= 0;i --)
{
value[ i ] = encodeURIComponent( value[ i ] );
}
var p = key + '=' + value.join( '&' + key + '=' );
return url + ( /\?/.test( url ) ? '&' : '?' ) + p;
}