JavaScript重定向使用包含”@ “的URL参数。

huangapple go评论55阅读模式
英文:

JavaScript redirect using URL parameters that contains "@"

问题

我试图使用JS函数在按钮点击时重定向到一个新页面。

URL应包含电子邮件地址参数。

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

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

我已经测试了我的代码,一旦我在URL参数中使用普通字符串,包含 "@" 和 "." 符号,代码就能正常工作。
但一旦我在URL参数中使用电子邮件地址,重定向就失败了。

console.log("https://rinateithan.com/thank-you?sender="+email); // 例如,返回 https://rinateithan.com/thank-you?sender=test@gmail.com

如何使用JavaScript重定向通过URL参数传递电子邮件地址
英文:

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

The URL should contain email address parameter.

&lt;form&gt;
&lt;input type=&quot;email&quot; id=&quot;mail&quot; placeholder=&quot;YOUR EMAIL ADDRESS&quot; required/&gt;
&lt;input type=&quot;submit&quot; value=&quot;SEND US THE LIST&quot; onclick=&quot;redirect();&quot;/&gt;

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

}
&lt;/script&gt;

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(&quot;https://rinateithan.com/thank-you?sender=&quot;+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?

答案1

得分: 1

@ 在 URL 中是不允许的

使用 encodeURIComponent

const email = 'test@gmail.com'
console.log("https://rinateithan.com/thank-you?sender="+encodeURIComponent(email));
英文:

@ is not allowed in the url

Use encodeURIComponent

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const email = &#39;test@gmail.com&#39;
console.log(&quot;https://rinateithan.com/thank-you?sender=&quot;+encodeURIComponent(email));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月24日 19:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定