2

I'm trying to redirect to a new page on button click using JS function.

The URL should contain email address parameter.

<form>
<input type="email" id="mail" placeholder="YOUR EMAIL ADDRESS" required/>
<input type="submit" value="SEND US THE LIST" onclick="redirect();"/>

<script>
function redirect(){
    var email = document.getElementById("mail").value;
    window.location.replace("https://rinateithan.com/thank-you?sender="+email);

}
</script>

I have tested my code, once I use normal string with "@" and "." symbols the code works fine. But once I use email address in the URL parameters the redirect fails.

Console.log("https://rinateithan.com/thank-you?sender="+email); // Returns https://rinateithan.com/thank-you?sender=test@gmail.com for example

How can I transfer email address through URL parameters using JavaScript redirection?

Yotam Dahan
  • 689
  • 1
  • 9
  • 33
  • 1
    Does this answer your question? [Encode URL in JavaScript](https://stackoverflow.com/questions/332872/encode-url-in-javascript) – Johannes Stadler Jul 24 '23 at 12:01
  • The keyword you're looking for is "URL encode", there's for example the `encodeURIComponent` function in Javascript that you can use to encode special chars like '@'. – Johannes Stadler Jul 24 '23 at 12:03

1 Answers1

1

@ is not allowed in the url

Use encodeURIComponent

const email = 'test@gmail.com'
console.log("https://rinateithan.com/thank-you?sender="+encodeURIComponent(email));
Konrad
  • 21,590
  • 4
  • 28
  • 64