javaScript中的编码方法:
escape() 方法:
采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。unescape方法与此相反。不会被此方法编码的字符: @ * / +

英文解释:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."
Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

encodeURIComponent() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。不会被此方法编码的字符:! * ( )

英文解释:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。

英文注释:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.

javaScript编码相关推荐

  1. JavaScript编码风格指南

    首次发表在个人博客 前言 程序语言的编码风格指南对于一个长期维护的软件而言是非常重要的;好的编程风格有助于写出质量更高.错误更少.更易于 维护的程序. 团队合作需要制定一些代码规范还有利用一些工具来强 ...

  2. Airbnb JavaScript 编码风格指南(2018年最新版)

    Airbnb JavaScript 编码风格指南(2018年最新版) 访问此原文地址:http://galaxyteam.pub/didi-fe... 另外欢迎访问我们维护的https://www.t ...

  3. 一些达成共识的JavaScript编码风格约定

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  4. JavaScript编码规范[百度]

    JavaScript编码规范 1 前言 2 代码风格 2.1 文件 2.2 结构 2.2.1 缩进 2.2.2 空格 2.2.3 换行 2.2.4 语句 2.3 命名 2.4 注释 2.4.1 单行注 ...

  5. JavaScript 编码规范大全-Eslint(持续更新,欢迎关注点赞加评论)

    JavaScript 编码规范大全(持续更新,欢迎关注点赞加评论) 文章目录 JavaScript 编码规范大全(持续更新,欢迎关注点赞加评论) 前言 0. 相关工具 1. 类型 2. 引用 3. 对 ...

  6. 网易前端JavaScript编码规范

    在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...

  7. javascript 编码_我们的1,600小时JavaScript编码课程

    javascript 编码 by freeCodeCamp 通过freeCodeCamp 我们的1,600小时JavaScript编码课程 (Our 1,600 Hour JavaScript Cod ...

  8. JavaScript => JavaScript编码规范指南

    JavaScript 编码规范指南 以下文档大多来自: Google JavaScript 编码规范指南 Idiomatic 风格 对于未提及的事项可以参考airbnb的JS编码规范 airbnb/j ...

  9. javascript编码规范

    一.前言 如果你写的代码只是给自己看,不需要与多人共同协作:如果你写的代码不打算开源:如果你不进行code review:如果你不打算担任前端工程师这一岗位,只是单纯业余爱好:满足以上任一条件,我建议 ...

  10. Javascript编码规范,好的代码从书写规范开始,增强代码的可读性,可维护性,这是相当重要的!...

    1. 前言 JavaScript在百度一直有着广泛的应用,特别是在浏览器端的行为管理.本文档的目标是使JavaScript代码风格保持一致,容易被理解和被维护. 虽然本文档是针对JavaScript设 ...

最新文章

  1. python selenium 元素定位_python3+selenium入门04-元素定位
  2. Cadence原理图导出智能PDF(带图页、位号与网络名书签且文本可搜索)
  3. 6号团队-团队任务4:每日立会(2018-11-28)
  4. Kafka和其他消息队列
  5. iOS 14.5刚推送,苹果隐私政策就遭反垄断投诉
  6. 算法题003 斐波那契(Fibonacci)数列
  7. 为Windows添加日志事件
  8. HTTP长连接和短连接(转)
  9. Pure Storage到底是一家怎样的企业?
  10. 原创 | 基于开源的互联网和深度信息技术
  11. headerIP php_PHP正确获取客户端IP地址
  12. 强制打开BIOS中禁用的I/OAT DCA(二)
  13. 【去除教育】去除打印教育戳记插件
  14. 操作系统概念 学习笔记
  15. 阿里优酷视频分类方法???咋理解啊?
  16. Hadoop-HDFS学习
  17. 废柴日记之国庆特辑:那些年我们一直分不清楚的近义词们②
  18. 怎么用python下载视频_用python一行代码批量下载哔哩哔哩视频
  19. 3D游戏里的男女性角色模型是这样建模出来的
  20. zk服务启动报错:Unexpected exception, exiting abnormally.java.io.IOException:

热门文章

  1. 梯度下降算法(GD)—收敛速率证明
  2. glob.glob()、sort() 等一些函数的用法
  3. pip:你真的熟悉怎么用了吗?
  4. 设计一个程序,完成(英雄)商品的购买(界面就是第一天打印的界面) 展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款 输入付款金额->打印购买小票
  5. c++修复工具_别再花钱修复旧照片啦!老照片一键修复工具,独创方法支持PS2020...
  6. Debug JDK源码没变量值怎么办?
  7. git如何移除某文件夹的版本控制
  8. 标准IT POD之美
  9. OraclePLSQL 安装及乱码问题解决
  10. 解方程求PH值,POJ(2006)