URL编码,有时也称为百分比编码,是一种将URL中的任何数据编码为可以在internet上传输的安全格式的机制。URL编码还用于为提交具有application/x-www-form-urlencoded MIME类型的HTML表单准备数据。
encodeURIComponent()
注意,不应该使用encodeURIComponent()函数对整个URL进行编码。它应该只用于对查询字符串或路径段进行编码:
var baseUrl = 'https://www.google.com/search?q='
var query = 'Hellö Wörld@Javascript'
// encode only the query string
var completeUrl = baseUrl + encodeURIComponent(query)
console.log(completeUrl)
// https://www.google.com/search?q=Hell%C3%B6%20W%C3%B6rld%40Javascript
encodeURI()
语法:encodeURI(URI)
参数URI:A complete URI.
const uri = 'https://mozilla.org/?x=шеллы';
const encoded = encodeURI(uri);
console.log(encoded);
// expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"
encodeURI vs encodeURIComponent
const set1 = ";,/?:@&=+$#"; // Reserved Characters
const set2 = "-_.!~*'()"; // Unreserved Marks
const set3 = "ABC abc 123"; // Alphanumeric Characters + Space
console.log(encodeURI(set1)); // ;,/?:@&=+$#
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // ABC%20abc%20123 (the space gets encoded as %20)
console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24%23
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // ABC%20abc%20123 (the space gets encoded as %20)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。