0

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

UPDATE - How can I also store a totalVal by flattening the array? So, if the array was ["chicken", "toast"] I need this flattened into a string with a "." before each item in the array - so if the array was ["chicken", "toast"] it would become ".chicken.toast" - if the array was ["chicken"] it would become ".chicken" // thanks

Cheers,

3 Answers 3

1

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();

Here's a version which gives the .chicken result:

var r = window.location.href.split("?");
r[0]='';
var totalval = r.join('.');
Sign up to request clarification or add additional context in comments.

7 Comments

OK thanks - then will need to flatten array into 2 vars yeh?
You can extract the values of the array how you like. I've edited to show one way.
Last question if that's OK... Can I flatten array into 1 variable, with the values seperated with "." - so it would be: var totalval = ".chicken.toast" if there were 2 paramaters in the string, and var totalval = ".chicken" if just one?
I've added a further edit to give the .chicken.toast output
Thanks Chris - I assume I can run the scripts so I can extract both a joined list of the array results - e.g. ".chicken.toast" as well as single variables - e.g. "chicken" and "toast"?
|
1

I'd suggest:

// obviously, in production you should use document.location.href:
var url = "http://www.example.com/?chicken?toast",

// take a substring of the url variable, starting at the index of
// the (first) '?' character and running to the end of the string,
// giving "?chicken?toast", we then split that resultant string
// on the '?' characters, and filter the resulting array using
// filter(Boolean), which retains only the true/truthy array-elements:
    values = url.substring(url.indexOf('?')).split('?').filter(Boolean);
console.log(values);

var url = "http://www.example.com/?chicken?toast",
    values = url.substring(url.indexOf('?')).split('?').filter(Boolean);
console.log(values);

You could, instead, use document.location.search to retrieve the substring from the first '?' onwards (if using document.location.href).

References:

3 Comments

Thanks - that drops them into an array yeh - so will then need to parse them from the array into 2 variables yeh?
Or simply address them as values[0], values[1]: they're already in an Array, you can access them using array[index] notation.
Thanks. All understood.
0
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i;

for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');
    // Do something with the name sParameterName[0]
    // Do something with the value sParameterName[1]
    }

This reference may be of some use to you: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.