Skip to content

Instantly share code, notes, and snippets.

@Cycymomo
Created April 15, 2014 08:50
Show Gist options
  • Save Cycymomo/10715000 to your computer and use it in GitHub Desktop.
Save Cycymomo/10715000 to your computer and use it in GitHub Desktop.
urlParameterUpdate.js
/* remplace ou ajoute des paramètres à une URL */
function urlParameterUpdate(id, value){
if (value == '' || id == '') return;
// Vérifie si l'url contient "id="
var oRegExp = new RegExp('&'+id+'(=[^&]*)?|^'+id+'(=[^&]*)?&?','g');
var oUrl = (window.location.href).toString();
// l'URL contient déja l'id
if (oUrl.match(oRegExp)) {
oUrl = oUrl.replace(oRegExp, '&'+id+'='+value);
} else {
// sinon on ajoute
oUrl += '&' + id + '=' + value ;
}
window.location.replace(oUrl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment