Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
What do you have so far? –  Todd Motto Jan 17 at 16:54
    
Duplicate question - stackoverflow.com/questions/901115/… –  Tom Cammann Jan 17 at 16:55
    
You may be able to reference: Append to URL and Refresh Page –  Craighead Jan 17 at 16:55
    
Perhaps take a look at: stackoverflow.com/questions/4656843/… –  David J Barnes Jan 17 at 16:56
    
if you have apache: stackoverflow.com/questions/11210412/… –  Fabrizio Calderan Jan 17 at 16:57

3 Answers 3

up vote 2 down vote accepted
if ( !window.location.search.trim().length ) 
   window.location.href = window.location.href + '?a=0';
share|improve this answer
    
Exactly what i was looking for!! –  user1610812 Jan 17 at 16:59
if(!(window.location.search.indexOf("?a=0") > -1)) {
    window.location.href += window.location.search;
}
share|improve this answer
    
Thanks! Perfect exactly what i was looking for. –  user1610812 Jan 17 at 16:58
    
@user1610812 - great. glad to help. Please upvote all helpful answers and accept one as the solution. –  Krishna Jan 17 at 16:59

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
    }

});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.