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.
Asked
Active
Viewed 729 times
0

nullability
- 10,545
- 3
- 45
- 63

monal86
- 433
- 2
- 11
- 26
-
2What do you have so far? – Todd Motto Jan 17 '14 at 16:54
-
Duplicate question - https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Tom Jan 17 '14 at 16:55
-
You may be able to reference: [Append to URL and Refresh Page](http://stackoverflow.com/questions/5997450/append-to-url-and-refresh-page) – Craighead Jan 17 '14 at 16:55
-
Perhaps take a look at: http://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url – David J Barnes Jan 17 '14 at 16:56
-
if you have apache: http://stackoverflow.com/questions/11210412/htaccess-301-redirect-if-no-query-string-present – Fabrizio Calderan Jan 17 '14 at 16:57
3 Answers
3
if(!(window.location.search.indexOf("?a=0") > -1)) {
window.location.href += window.location.search;
}

Venkata Krishna
- 14,926
- 5
- 42
- 56
-
@user1610812 - great. glad to help. Please upvote all helpful answers and accept one as the solution. – Venkata Krishna Jan 17 '14 at 16:59
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