<span>var url = $(location).attr('href'); //get current url </span><span>//OR </span><span>var url = 'folder/index.html?param=#23dd&noob=yes'; //or specify one </span> <span>var encodedUrl = encodeURIComponent(url); </span><span>console.log(encodedUrl); </span><span>//outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes</span>
<span>var url = $(location).attr('href'); //get current url </span><span>//OR </span><span>var url = 'folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes'; //or specify one </span> <span>var decodedUrl = decodeURIComponent(url); </span><span>console.log(decodedUrl); </span><span>//outputs folder/index.html?param=#23dd&noob=yes</span>
space %20 ! %21 " %22 # %23 $ %24 % %25 & %26 ' %27 ( %28 ) %29 * %2A + %2B , %2C - %2D . %2E / %2F ... etc
>
在jQuery中解释url,您可以使用DecoDeurIricompents()函数()函数。此功能可以解码URL的任何编码组件,从而返回原始字符串。这是一个简单的示例:
var url =“ https://www.example.com”;
var decodedUrl = decodeUricomponent(url);
>您可以在jQuery中使用$ ..ajax()方法来发送ajax请求,以将ajax请求发送带有URL参数。发送之前,您应该编码URL参数,以确保它们正确格式化。以下是一个示例:
$。ajax({
url:'https://www.example.com',
data:{param1:encodeuricomponents(valion1),param2:encodeuricompongents(encodeuricompongents}(value2)},(value2)},
成功: }
});
>
jQuery不提供自己的URL编码或解码的方法。但是,您可以在您的jQuery代码中使用javaScript函数encodeuricomponent(),decodeuricomponent(),ecdodeuri()和decodeuri(),decodeuri()和decodeuri()。
>> decodeuricomponent函数在jQuery?
>>如何防止JavaScript通过URL参数?
我可以使用URL编码来传递URL中的数组或对象?但是,这可能很复杂,可能需要在服务器端进行其他处理以正确解释编码的数据。通常使用POST请求或通过将数据转换为JSON字符串来传递复杂的数据结构通常更容易。
以上是jQuery编码/解码URL字符串的详细内容。更多信息请关注PHP中文网其他相关文章!