dubbs dubbs - 1 year ago 129
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


Answer Source

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

var r = window.location.href.split("?")

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

var val1 = r.shift();
var val2 = r.shift();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download