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.

5

3 Answers 3

3
if(!(window.location.search.indexOf("?a=0") > -1)) {
    window.location.href += window.location.search;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user1610812 - great. glad to help. Please upvote all helpful answers and accept one as the solution.
2
if ( !window.location.search.trim().length ) 
   window.location.href = window.location.href + '?a=0';

Comments

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
    }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.