0

Need some help below:

Page 1 URL: www.example.com?name=emily&email=example@gmail.com

My current code on www.example.com/index.html:

<html> 
<head>
<meta http-equiv="Refresh" content="0;url=www.test.com">
</head>
</html>

How do I write a code to pass the value of "name" and "email" to another URL?

It will look something like below

Desired Page 2 URL: www.test.com?name=emily&email=example@gmail.com

Sorry guys, I'm quite new to html and Javascript, please help me to modify my code by adding the required code so I can just use it and run. Thanks in advance!

Helmy
  • 19
  • 3

3 Answers3

0

You can use window.location.search to get the query string and parse it with `URLSarchParams

let queryString = window.location.search;

let urlParams = new URLSearchParams(queryString)

let name = urlParams.get('name') // get name 
let email = urlParams.get('email') // get email

let newUrl = `www.test.com?name=${name}&email=${email}` // build new url

Update get meta tag value using post https://stackoverflow.com/a/7524621/14959189:

function getMeta(metaName) {
  const metas = document.getElementsByTagName('meta');


  for (let i = 0; i < metas.length; i++) {
    if (metas[i].getAttribute('name') === metaName) {
      return metas[i].getAttribute('content');
    }
  }

  return '';
}

console.log(getMeta('myUrl'));
<html>

<head>
  <meta name="myUrl" http-equiv="Refresh" content="www.test.com">
</head>

</html>
  • Hi, thanks for your prompt reply. My current code on www.example.com/index.html is below: Could you help me to rewrite my code in order to pass the value to www.test.com, sorry I'm beginner to coding – Helmy Jan 26 '21 at 11:24
  • I don't know why you would store that in a meta tag, but take a look at this post here https://stackoverflow.com/a/7524621/14959189 –  Jan 26 '21 at 11:32
  • Thanks for sharing, not too sure how to apply it to my case... – Helmy Jan 26 '21 at 11:39
  • Here you go I made an example –  Jan 26 '21 at 11:54
  • Hi, thanks for your example, I tested it but the code don't see work, couldn't see any values passing, https://www.codepile.net/pile/1oW8LgYP – Helmy Jan 26 '21 at 12:23
  • Try it in your code not on that website, also you can run it here too there is output, click on the run snippet. –  Jan 26 '21 at 12:36
0

You could do something like

function appendQueryParams(sourceUrl, destinationUrl) {
  const url = new URL(sourceUrl);
  return `${destinationUrl}${url.search}`;
}

const newUrl = appendQueryParams('https://www.example.com?name=emily&email=example@gmail.com', 'https://www.test.com');
blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • Hi, thanks for your prompt reply. My current code on www.example.com/index.html is below: Could you help me to rewrite my code in order to pass the value to www.test.com, sorry I'm beginner to coding – Helmy Jan 26 '21 at 11:26
  • yea.. i tried using `new URL` except when you take away the http or https it just throws an error – The Bomb Squad Jan 26 '21 at 11:31
  • @TheBombSquad sorry cannot understand your comments – Helmy Jan 26 '21 at 11:34
0

<script>
function replaceOrigin(url,newOrigin){
  var splittingChar=""
  if(url.includes("/")){
    url=url.split("/"); splittingChar="/"
  }
  else{
    url=url.split("?"); splittingChar="?"
  }
  url=url[url.length-1] //ensures that url is ONLY the params
  var newUrl=newOrigin+splittingChar+url //joins the params from previous url to new origin
  return(newUrl)
}



//example usage
var url1="www.example.com?name=emily&email=example@gmail.com"
var myUrl=replaceOrigin(url1,"www.test.com") //you can replace anyhow you want
console.log(myUrl)

var x=document.createElement('meta')
x.httpEquiv="Refresh"
x.content=`0;${myUrl}`
document.body.appendChild(x)
//you would see something flash for a bit because something was logged on the console but then the url changed thanks to the meta tag..
</script>
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17
  • Hi, thanks for your prompt reply. My current code on www.example.com/index.html is below: Could you help me to rewrite my code in order to pass the value to www.test.com, sorry I'm beginner to coding – Helmy Jan 26 '21 at 11:34
  • ok.. u can save that as a pure .html file and it'll run @Helmy – The Bomb Squad Jan 26 '21 at 11:39
  • I uploaded my code here, https://www.codepile.net/pile/Gn2OmxwE , really appreciate for your help. – Helmy Jan 26 '21 at 11:45