Peter Olson
Peter Olson

Reputation: 142911

Set Reference to document.location.search

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

Answers (2)

DanielB
DanielB

Reputation: 20210

Write a function

function s(val) {
    document.location.search = "?a=" + val;
}

and call

s('test');

Upvotes: 5

gnur
gnur

Reputation: 4733

You could shorten it a bit more by setting it like this:

s=document.location
s.search="?a="+n;

Upvotes: 2

Related Questions