Note that defining (and setting the values of) the variables outside the function like that will probably return an error because the page won't have finished loading when the script is executed. Either that or the "theQuery.." variables will all be empty since document.getElementById("") won't return a list of elements if it can't find them.
You would only need to do
outside the function to make those variables global. Keep the rest of the code in the function but remove the var before the "theQuery.." variables or they will still be declared as private to that function.
That way, you can change any part of the query string any time by simply doing
and the complete url string will be recreated inside the function each time it runs.