Reputation: 142911
Throughout my code, I have statements like this:
document.location.search="?a="+n;
I am trying to shorten (for code golf) it so that I do not have to type out the document.location.search
every time.
The first thing I thought of was to set document.location.search
to a variable, and then setting that variable, like this:
s=document.location.search;
s="?a="+n;
This, of course, does not work, because s is set by the value of document.location.search
, and not its reference. Is there a way to set s by reference? Is there some other way I can eliminate repeated instances of document.location.search
?
Upvotes: 1
Views: 1757
Reputation: 20210
Write a function
function s(val) {
document.location.search = "?a=" + val;
}
and call
s('test');
Upvotes: 5
Reputation: 4733
You could shorten it a bit more by setting it like this:
s=document.location
s.search="?a="+n;
Upvotes: 2