繁体   English   中英

如何在=登录jQuery后获取url的值

[英]How to get the value of url after = sign in jQuery

我想在等号后获取jquery中的值,但是我无法获取这是我的url的值

https://dev.example.com/front-end-pm/?fepaction=newmessage&=Shammy%20Kothari

这就是我试图获取=符号后的值的方式

var url      =window.location.href; 
var queryString = url.split('&', 2)[1] || '';

我越来越像这样

麂皮=%20Kothari

我只需要

沙米·科塔里(Shammy Kothari)

您需要使用decodeURIComponent()对URL进行解码

 //var url =window.location.href; var url ="https://dev.example.com/front-end-pm/?fepaction=newmessage&=Shammy%20Kothari"; var str = decodeURIComponent(url.split('=').pop()); console.log(str); 

您可能要更改的第一件事是url中的值未分配给名称。 看起来像这样: https://dev.example.com/front-end-pm/?fepaction=newmessage&YOUR_PARAMETER_NAME=Shammy%20Kothari ://dev.example.com/front-end-pm/?fepaction=newmessage&YOUR_PARAMETER_NAME=Shammy%20Kothari之后,您可以按照此处建议的那样使用URLSearchParams。 看起来可能如下所示:

const urlParams = new URLSearchParams(window.location.search);
//If you don't want to retrieve the url from the url bar just use: const urlParams = new URLSearchParams('https://dev.example.com/front-end-pm/?fepaction=newmessage&YOUR_PARAMETER_NAME=Shammy%20Kothari');
const myParam = urlParams.get('YOUR_PARAMETER_NAME'); //would be 'Shammy Kothari'

暂无
暂无

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

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