如何在jQuery中获取URL的值



我想在平等标志之后以jQuery获取值,但我无法获得值,这是我的url

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

这就是我在= sign

之后尝试获得值的方式
var url      =window.location.href; 
var queryString = url.split('&', 2)[1] || '';

我变得像这样

= shammy%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之后,您可以按照此处提出的urlsearchParams使用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'

最新更新