• js对url进行编码解码(三种方式)


    方法说明返回值
    escape(String)使用转义序列替换某些字符来对字符串进行编码,除了ASCII字母、数字、标点符号"@ * _ + - . /"以外返回Unicode编码字符串
    unescape(String)对使用 escape() 编码的字符串进行解码
    encodeURI(String)通过转义某些字符对 URI 进行编码,除了常见的符号以外(ASCII 字符),对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码输出utf-8形式字符串
    decodeURI(String)对使用 encodeURI() 方法编码的字符串进行解码
    encodeURIComponent(String)通过某些转义字符对 URI 进行编码,会编译所有(包含特殊字符),ASCII 字符不编码,可以将参数中的中文、特殊字符进行转义输出utf-8形式字符串
    deencodeURIComponent(String)对使用 encodeURIComponent() 方法编码的字符串进行解码

    第一种:escape 和 unescape

    escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值

    它的具体规则是,除了ASCII字母、数字、标点符号"@ * _ + - . /"以外,对其他所有字符进行编码。在u0000到u00ff之间的符号被转成%xx的形式,其余符号被转成%uxxxx的形式。对应的解码函数是unescape()。

    还有两个点需要注意:

    1. 首先,无论网页的原始编码是什么,一旦被Javascript编码,就都变为unicode字符。也就是说,Javascipt函数的输入和输出,默认都是Unicode字符。这一点对下面两个函数也适用。
    2. 其次,escape()不对 "+" 编码。但是我们知道,网页在提交表单的时候,如果有空格,则会被转化为+字符。服务器处理数据的时候,会把+号处理成空格。所以,使用的时候要小心。
    1. escape()编码:
    2. const time = 2022-01-09
    3. const tile = '63元黑糖颗粒固饮'
    4. let url = "http://localhost:8080/index.html?time="+escape(time)+"&title="+escape(tile)
    5. 地址栏显示结果:
    6. "http://localhost:8080/index.html?time=2022-01-09&title=63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E"
    1. unescape()解码:
    2. let url = "http://localhost:8080/index.html?time="+unescape(2022-01-09)+"&title="+unescape(63%u5143%u9ED1%u7CD6%u9897%u7C92%u56FA%u996E)
    3. 地址栏显示结果:
    4. "http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"

    第二种:encodeURI 和 decodeURI

    encodeURI()是Javascript中真正用来对URL编码的函数。

    它用于对URL的组成部分进行个别编码,除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = + $ , #",也不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%,,然后用十六进制的转义序列(形式为%xx)对生成的 1 字节、2 字节或 4 字节的字符进行编码。
    它对应的解码函数是decodeURI()

    需要注意的是,它不对单引号'编码

    1. let url = "http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"
    2. encodeURI()编码:
    3. let encodeURI_url = encodeURI(url) = "http://localhost:8080/index.html?time=2022-01-09&title=63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE"
    4. decodeURI()解码:
    5. decodeURI(encodeURI_url )= “http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”

    第三种:encodeURIComponent 和 decodeURIComponent

    与encodeURI()的区别是,它用于对整个URL进行编码。"; / ? : @ & = + $ , #",这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。
    它对应的解码函数是decodeURIComponent()。

    1. let url = "http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮"
    2. encodeURIComponent ()编码:
    3. let encodeURIComponent _url = encodeURIComponent (url) = http%3A%2F%2Flocalhost%3A8080%2Findex.html%3Ftime%3D2022-01-09%26title%3D63%E5%85%83%E9%BB%91%E7%B3%96%E9%A2%97%E7%B2%92%E5%9B%BA%E9%A5%AE
    4. decodeURIComponent()解码:
    5. decodeURIComponentencodeURIComponent _url )= “http://localhost:8080/index.html?time=2022-01-09&title=63元黑糖颗粒固饮”

  • 相关阅读:
    旅游网站毕业设计,旅游网站网页设计设计源码,旅游网站设计毕业论文
    python一点通:数据处理顶流Pandas 2.0有什么新功能?
    SimCSE:对比学习,只需要Dropout
    自定义组件-behaviors
    PHP-Composer包开发、发布流程
    子序列宽度之和
    vue 不相干的两个页面相互通信方式
    QT day03
    msvc++中的预编译头文件pch.hpp和stdafx.h
    【Linux】线程的概念
  • 原文地址:https://blog.csdn.net/muzidigbig/article/details/127750299