简体   繁体   English

如何从URL参数获取base64编码的值并将其放在使用JavaScript的输入字段中

[英]How to get base64 encoded value from URL parameters and place it in an input field with JavaScript

I want to use (base64 encoded key) as URL parameter 我想使用(base64编码的密钥)作为URL参数

like: mydomain.com/?key=cHVyY2hhc2VwcmljZT0zNCQ= 像:mydomain.com/?key=cHVyY2hhc2VwcmljZT0zNCQ=

that returns: purchaseprice=34$ 返回:purchaseprice = 34 $

I read How to get base64 encoded... but it did not help. 我读了《 如何获取base64编码》,但是它没有帮助。

here is my working code without base64: 这是我没有base64的工作代码:

 <input type="text" id="price" /> <script> const url = new URL(window.location); document.querySelector('#price').value = url.searchParams.get('purchaseprice'); </script> 

and in Codepen 并在Codepen中

If you're expecting the return value of purchaseprice to be base64 encoded then you'll need to use the atob() function: 如果您期望purchaseprice的返回值是base64编码的,则需要使用atob()函数:

<input type="text" id="price" />
<script>
const url = new URL(window.location);
document.querySelector('#price').value = atob(url.searchParams.get('purchaseprice'));
</script>

Details here: Base64 Encoding / Decoding in JavaScript . 此处的详细信息: JavaScript中的Base64编码/解码

Example: CodePen 示例: CodePen

thanks to cptcoathanger 感谢cptcoathanger

i did it With this Simple code. 我用这个简单的代码做到了。

 var fdurl = new URL(window.location); var fdunc = fdurl.searchParams.get('key'); var url = "https://codepen.io/shahabarvin/pen/JmzyZq?" + window.atob(fdunc); document.querySelector('#price').value = new URL(url).searchParams.get("purchaseprice"); 
 <input type="text" id="price" /> 

Codepen Codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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