简体   繁体   中英

Get a querystring from URL and pass as querystring to new URL using javascript

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM