Essayez celui-ci
/**
* Get the value of a querystring
* @param {String} field The field to get the value of
* @param {String} url The URL to get the value from (optional)
* @return {String} The field value
*/
var getQueryString = function ( field, url ) {
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
};
Disons que votre URL est http://example.com&this=chicken&that=sandwich . Vous voulez obtenir la valeur de ceci, cela, et autre.
var thisOne = getQueryString('this'); // returns 'chicken'
var thatOne = getQueryString('that'); // returns 'sandwich'
var anotherOne = getQueryString('another'); // returns null
Si vous voulez utiliser une URL autre que celle de la fenêtre, vous pouvez la passer comme deuxième argument.
var yetAnotherOne = getQueryString('example', 'http://another-example.com&example=something'); // returns 'something'
Référence
0 votes
Vérifiez ceci stackoverflow.com/questions/901115/
0 votes
@Cupcake : Cette question concerne l'extraction des paramètres, celle-ci ne concerne que l'obtention des paramètres.
location.search
0 votes
Voter pour rouvrir, le duplicata marqué est une demande de bibliothèque, alors que cette question vise à obtenir du code js.
0 votes
Duplicata possible de Comment obtenir la valeur des paramètres GET ?
0 votes
Cela répond-il à votre question ? Comment puis-je obtenir les valeurs des chaînes de requête en JavaScript ?