javascript - 为什么我要检索的 URL 参数没有完全显示?
问题描述
我正在使用“posName”参数跟踪 URL,例如:
example.com?posName=内容+&+社区+经理+(H/F)
但我的代码只返回“内容”:
function () {
var uri = document.location.href;
var uri_dec = decodeURIComponent(uri);
var url = new URL(uri_dec);
var posName= url.searchParams.get("posName");
return posName;
}
我怎样才能得到完整的参数?
编辑: 我有另一个网址,如:
example.com?posName= Club+&+Animator+(H/F)
并且代码返回完整的参数......那么是导致这个问题的参数的长度吗?
解决方案
我已修改您的函数以接受 URI 作为参数。您需要在将参数附加到查询字符串之前对其进行编码(我们在这里看不到此代码)。函数中的 decodeURIComponent 也不是必需的。
function getPosName (URI) {
//var uri_dec = decodeURIComponent(uri);
var url = new URL(URI);
var posName = url.searchParams.get("posName");
return posName;
}
console.log("Using your current URI:", getPosName("http://exemple.com?posName=Content+&+Community+Manager+(H/F)"))
const encodedParam = encodeURIComponent("Content & Community Manager (H/F)")
console.log("Encoded Parameter:", encodedParam)
const wellFormedURI = `http://exemple.com?posName=${encodedParam}`
console.log("Well Formed URI:", wellFormedURI)
console.log("Using well formed URI:", getPosName(wellFormedURI))
推荐阅读
- angular - 如何为 Tomcat 配置 Nginx proxy_pass in
- reactjs - Uncaught (in promise) TypeError: loginMutation is not a function
- java - 无限物品recyclerview练习
- android - 如何将数组中的所有值添加到 ArrayList?
- c++ - 我们可以声明一个抽象方法来返回抽象超类中的子类对象而不需要新的实例化吗?
- c++ - 在这种情况下,数组 a 会被取消分配吗?
- python - 如何解决使用 fmin_l_bfgs_b 后出现的“未能初始化意图(inout|inplace|cache)数组,输入不是数组”?
- flutter - 如何分享一个用 Dart 编写的闭源 Flutter 库?
- python - 在 Python 中仅获取列表的某些值
- c++ - C++20 标准::原子
- 标准::原子 .specializations