How do you access query string variables?

Hello,

I have found many there are many ways to retrieve a querystring variable, and people have posted many functions out there. What would be the correct way to go?

:slight_smile:

It depends.

The querystring is available via the window.location object, as window.location.search
Everything else stems from that.

The question mark at the start is of no use to you.
The terms are divided by ampersands.
Each term is divided by the equals sign.

So itโ€™s up to you to decide whether you want to split things up and interpret them accordingly, or to use a small library to do the job for you.

The following seems to provide good details about this: Javascript Madness: Query String Parsing

Thanks a lot :slight_smile:

How would you use the JMadness library? Iโ€™m trying to do this:


if(qs.value('test') == true) {
	alert('found');
	}

:slight_smile:

Like this:


var qs = new querystring();
if (qs.value('foo') === 'bar') {
    ...
}

:tup: