0

I need to Get a querystring from URL1 then redirect to URL2 - with the querystring from URL1 appended.

The querystring has 2 values:

http://URL1.com/hi/j?a=123&b=666

I want this to redirect to:

http://URL2.com/hi/b?a=123&b=666

I can't find anything on doing this on Google. I found this but it only works with 1 value and seems to have got criticism - and it only takes the querystring and doesn't add it: How can I get query string values in JavaScript?

Thanks.

niico
  • 11,206
  • 23
  • 78
  • 161

1 Answers1

2

try this pattern \?(.*) . Match the regex pattern after ? .Then paste with newly created url extenstion

Add your code as like this

var a =window.location.href;
var after =/\?(.*)/g.exec(a)[0]
window.location.href='http://URL2.com/hi/b'+after

Working example

var a ='http://URL1.com/hi/j?a=123&b=666';
var after =/\?(.*)/g.exec(a);
console.log(after[1].split('&'))//try with your wish from array(selectquery)
console.log('http://URL2.com/hi/b'+after[0])//for whole query
prasanth
  • 22,145
  • 4
  • 29
  • 53
  • thanks - any chance of making it do the redirection on page load javascript isn't my strong point thanks :) – niico Jun 09 '17 at 04:41
  • great thanks - just one more thing, - can you add a variation that only has 1 querystring value, thx. – niico Jun 09 '17 at 04:56
  • I was added with snippet. create the array with all query string .You could chose with respected one – prasanth Jun 09 '17 at 05:16