0

I'm trying to get the value of a variable in the query string example.com/?hop=test

And then pass it to a JavaScript in this form:

var exitsplashpage = 'http://example2.com/?hop=original_hop';

How do I get the original hop variable value using JavaScript and what is the correct format to put it into the exitsplashpage var?

Thanks!

2

2 Answers 2

1

Using this getQueryString() function from the link that @Felix Kling posted, you would do the following:

var exitsplashpage = 'http://example2.com/?hop=' + getQueryString()["hop"];

Either that, or have a look at jQuery Query String Object, with which you'd do this:

var exitsplashpage = 'http://example2.com/?hop=' + $.query.get("hop");
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var exitsplashpage = window.location.search.match(/\?hop=([^\&]*)/)[1]

4 Comments

This is unsafe for more than one URL parameter.
I'd use ?|& to allow ?foo=bar&hop=poh&bar=foo. Also OP is checking against hop. But that is kinda obvious.
Calm down, simple mistake on my part. But if you want to go there, I would say it's also kinda obvious that the OP was not asking for ?foo=bar&hop=poh&bar=foo.
Thank you. This worked with a minor modification: var exitsplashpage = 'http://example2.com/?hop=' + window.location.search.match(/[\?\&]hop=([^\&]*)/)[1]; needed to include the redirect URL and I didn't mention, but the hop variable might not be the first in the query string.

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.