此功能有效,它允许您选择所需的方式参数。
var exampleStrng ='trump=15&id=9616071454&hilarry=es'; // this is an example query string.
var urlQry = window.document.location.search.substring(1); // you can use this in live code to get the query string.
// reusable function for split in text.
function strSpliter( str, splitVal ){
return str.split(splitVal);
}
// function to reassign query parameter values.
function changQry(qry, setParam, chngVal){
var pnt = strSpliter(qry, '&'),//use the spliter function to change the query into an array split on the '&' character.
newQryArr = [], // a temp array to hold the new parameters and their value.
newQry = '',// this will be the string where the query parameters and values are reconstructed into a string.
newQryStr = '';// this will be the query with the new value.
pnt.forEach( function( item, idx ){
var param = strSpliter(item, '='); //use the spliter function to split the parameter and their value.
// checks the parameter against the one you want to change.
if( param[0] === setParam ){
param[1] = chngVal;// assigns the new value to the parameter.
newQryArr.push(param.join('=')); // rejoins the parameter and its value and pushes it into the temp array.
} else {
newQryArr.push(param.join('='));// rejoins the parameter and its value and pushes it into the temp array.
}
newQry = newQryArr.join('&');// rejoins all the parameters and their values.
newQryStr = '?' + newQry;// creates the new query string.
});
return newQryStr; // returns the new search query string.
}
changQry(exampleStrng, 'id', 77777745);
没有 cmets
var urlQry = window.document.location.search.substring(1);
function strSpliter( str, splitVal ){
return str.split(splitVal);
}
function changQry(qry, setParam, chngVal){
var pnt = strSpliter(qry, '&'),
newQryArr = [],
newQry = '',
newQryStr = '';
pnt.forEach( function( item, idx ){
var param = strSpliter(item, '=');
if( param[0] === setParam ){
param[1] = chngVal;
newQryArr.push(param.join('='));
} else {
newQryArr.push(param.join('='));
}
newQry = newQryArr.join('&');
newQryStr = '?' + newQry;
});
return newQryStr;
}
changQry(urlQry, 'id', 77777745);