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!

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
Dustin
  • 79
  • 1
  • 4
  • 12

2 Answers2

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");
Community
  • 1
  • 1
Town
  • 14,706
  • 3
  • 48
  • 72
0

Try this:

var exitsplashpage = window.location.search.match(/\?hop=([^\&]*)/)[1]
clmarquart
  • 4,721
  • 1
  • 27
  • 23
  • This is unsafe for more than one URL parameter. – Eliasdx Apr 14 '11 at 00:13
  • I'd use ?|& to allow `?foo=bar&hop=poh&bar=foo`. Also OP is checking against `hop`. But that is kinda obvious. – Eliasdx Apr 14 '11 at 00:19
  • 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`. – clmarquart Apr 14 '11 at 00:23
  • 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. – Dustin Apr 14 '11 at 17:30