dubbs dubbs - 5 months ago 15
Javascript Question

jQuery setting variables from url string

I have a URL string like http://www.example.com/?chicken?toast

And I want to be able to store the values after each of the ? characters.

I have it working getting the last ?value - but cant seem to store the first...

Code that works to get the last ?value is:

window.lastValue = window.location.href.substring(window.location.href.lastIndexOf('?') + 1);


How can I store the first also?

So firstValue = chicken and lastValue = toast

Cheers,

Answer

This will give you an array of the values you want:

var r = window.location.href.split("?")
r.shift();
console.log(r);

If there are always exactly two values, you can use this to extract them:

var val1 = r.shift();
var val2 = r.shift();
Comments