Skip to content

15.什么是encodeURIComponent() 函数? #15

@webVueBlog

Description

@webVueBlog
Member

定义和用法

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

语法

encodeURIComponent(URIstring)
参数 描述
URIstring 必需。一个字符串,含有 URI 组件或其他要编码的文本。

返回值

URIstring 的副本,其中的某些字符将被十六进制的转义序列进行替换。

说明

该方法不会对 ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ' ( )

其他字符(比如 :;/?:@&=+$,# 这些用于分隔 URI 组件的标点符号),都是由一个或多个十六进制的转义序列替换的。

提示和注释

提示:请注意 encodeURIComponent() 函数 与 encodeURI() 函数的区别之处,前者假定它的参数是 URI 的一部分(比如协议、主机名、路径或查询字符串)。因此 encodeURIComponent() 函数将转义用于分隔 URI 各个部分的标点符号。

实例

接下来我将使用 encodeURIComponent()URI 进行编码:

<script type="text/javascript">

document.write(encodeURIComponent("http://www.web.com.cn"))
document.write("<br />")
document.write(encodeURIComponent("http://www.web.com.cn/p 1/"))
document.write("<br />")
document.write(encodeURIComponent(",/?:@&=+$#"))

</script>

输出:

http%3A%2F%2Fwww.web.com.cn
http%3A%2F%2Fwww.web.com.cn%2Fp%201%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @webVueBlog

        Issue actions

          15.什么是encodeURIComponent() 函数? · Issue #15 · weekCodeing/interview-answe