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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks - any chance of making it do the redirection on page load javascript isn't my strong point thanks :)
great thanks - just one more thing, - can you add a variation that only has 1 querystring value, thx.
I was added with snippet. create the array with all query string .You could chose with respected one

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.