我应该使用encodeURI或encodeURIComponent来编码URL吗? [英] Should I use encodeURI or encodeURIComponent for encoding URLs?

查看:109
本文介绍了我应该使用encodeURI或encodeURIComponent来编码URL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种方法中的哪一种应该用于编码网址?

Which of these two methods should be used for encoding URLs?

推荐答案

这取决于你实际想要做什么。

It depends on what you are actually wanting to do.

encodeURI假设输入是一个完整的URI,可能包含一些需要编码的字符。

encodeURI assumes that the input is a complete URI that might have some characters which need encoding in it.

encodeURIComponent将对具有特殊含义的所有内容进行编码,因此您将其用于URI的组件,例如

encodeURIComponent will encode everything with special meaning, so you use it for components of URIs such as

var world = "A string with symbols & characters that have special meaning?";
var uri = 'http://example.com/foo?hello=' + encodeURIComponent(world);

这篇关于我应该使用encodeURI或encodeURIComponent来编码URL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆