公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

任何网站的 URL 都需要对 URI 和 URI 组件进行编码和解码,以到达或重定向用户。这是 Web 开发中的一项常见任务,通常是在使用查询参数向 API 发出 GET 请求时完成的。查询参数还必须编码在 URL 字符串中,服务器将对其进行解码。许多浏览器会自动对 URL 和响应字符串进行编码和解码。

例如,空格“ ”被编码为 + 或 %20。

对 URL 进行编码

可以使用 JavaScript 中的以下方法来完成特殊字符的转换 –

    encodeURI() 函数 – encodeURI() 函数用于对完整的 URI 进行编码,即将 URI 中的特殊字符转换为浏览器可理解的语言。一些未编码的字符是:(, / ? : @ & = + $ #)。

    encodeURIComponent() 函数 – 这函数对整个 URL 而不仅仅是 URI 进行编码。该组件还对域名进行编码。

    语法

    encodeURI(complete_uri_string )
    encodeURIComponent(complete_url_string )

    登录后复制

    参数

      complete_uri_string string – 它保存要编码的URL。

    • complete_url_string string – 它保存要编码的完整 URL 字符串。

      上述函数返回编码后的 URL。

      示例 1

      在下面的示例中,我们使用encodeURI() 和encodeURIComponent() 方法对URL 进行编码。

      # index.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
         <title>Encoding URI</title>
      </head>
      <body>
         <h1 style="color: green;">
            Welcome To Tutorials Point
         </h1>
         <script>
            const url="https://www.tutorialspoint.com/search?q=java articles";
            document.write('<h4>URL: </h4>' + url)
            const encodedURI=encodeURI(url);
            document.write('<h4>Encoded URL: </h4>' + encodedURI)
            const encodedURLComponent=encodeURIComponent(url);
            document.write('<h4>Encoded URL Component: </h4>' + encodedURLComponent)
         </script>
      </body>
      </html>

      登录后复制

      输出

      解码 URL

      URL 的解码可以使用以下方法完成 –

        decodeURI() function -decodeURI() 函数用于解码 URI,即将特殊字符转换回原始 URI 语言。

        decodeURIComponent( ) 函数 – 此函数将完整的 URL 解码回其原始形式。 decodeURI 仅解码 URI 部分,而此方法解码 URL,包括域名。

        语法

        decodeURI(encoded_URI )
        decodeURIComponent(encoded_URL

        登录后复制

        参数

          encoded_URI URI – 它接受由encodeURI()函数创建的编码URL的输入。

          encoded_URL URL – 它接受由encodeURIComponent()函数创建的编码URL的输入。

          这些函数将返回编码 URL 的解码格式。

          示例 2

          在下面的示例中,我们使用decodeURI()和decodeURIComponent()方法将编码 URL 解码为它的编码 URL。原始形式。

          #index.html

          <!DOCTYPE html>
          <html lang="en">
          <head>
             <title>Encode & Decode URL</title>
          </head>
          <body>
             <h1 style="color: green;">
                Welcome To Tutorials Point
             </h1>
             <script>
                const url="https://www.tutorialspoint.com/search?q=java articles";
                const encodedURI = encodeURI(url);
                document.write('<h4>Encoded URL: </h4>' + encodedURI)
                const encodedURLComponent = encodeURIComponent(url);
                document.write('<h4>Encoded URL Component: </h4>' + encodedURLComponent)
                const decodedURI=decodeURI(encodedURI);
                document.write('<h4>Decoded URL: </h4>' + decodedURI)
                const decodedURLComponent = decodeURIComponent(encodedURLComponent);
                document.write('<h4>Decoded URL Component: </h4>' + decodedURLComponent)
             </script>
          </body>
          </html>

          登录后复制

          输出

          以上就是如何在 JavaScript 中对 URL 进行编码和解码?的详细内容,更多请关注www.92cms.cn其它相关文章!

分享到:
标签:javascript 中对 如何在 编码 解码
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定