0

How do I append a query string such as ?a=0 on document ready? It must first check if there is an existing query string. It should only append the query string if there isn't one there already. Otherwise, it should do nothing.

nullability
  • 10,545
  • 3
  • 45
  • 63
monal86
  • 433
  • 2
  • 11
  • 26

3 Answers3

3
if(!(window.location.search.indexOf("?a=0") > -1)) {
    window.location.href += window.location.search;
}
Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
2
if ( !window.location.search.trim().length ) 
   window.location.href = window.location.href + '?a=0';
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

Try this:

$( document ).ready(function() {

    url = window.location;  //get current url
    if(url.indexOf("?a=") == -1){ //check for ?a= 
     document.location = url+"?a=0"; // redirect it
    }

});
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53