1

i need to add new name ,value pair to existing query string when the user click on some button.

i'm using jquery for client side operations.

any idea..?

thank in advance!

Dinesh
  • 45
  • 3

2 Answers2

1

You could do:

$('#yourId').click(function(){
   var href = window.location.href;
var indexOfCanc = href.indexOf('#');
if(indexOfCanc === -1){
  if(href.indexOf('?') === -1){
     href+="?newparamter=my";
  }else{
     href+="&newparamter=my";
  }
 }else{
  var newHref = href.substring(0, indexOfCanc);
  var locationHash = href.substring(indexOfCanc); 
  if(href.indexOf('?') === -1){
     newHref += "?newparamter=my"+locationHash;
  }else{
     newHref += "&newparamter=my"+locationHash;
  }
 }   


   //use the new href for example reload the page with the new parameter:
   window.location.href = href;
});
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

Using regExp:

var href = window.location.href,
    toAdd,
    pattern1 = /\?/gi,
    pattern2 = /#/gi;
if (href.match(pattern1) != null) { toAdd = "&" }
else { toAdd = "?" }
toAdd += "param=val";
if (href.match(pattern2) != null) {
    var arr = href.split('#');
    href = arr[0] + toAdd + '#' + arr[1];
}
else { href += toAdd; }
AlexBay
  • 1,323
  • 2
  • 14
  • 26