JavaScript encodeURIComponent() 函数

encodeURIComponent -- 转义某些字符串对URI的组件编码
  • encode,中文"编码"的意思
  • 引用网址:http://www.dreamdu.com/javascript/encodeURIComponent/

encodeURIComponent函数语法

encodeURIComponent(str);

encodeURIComponent函数参数

  • str -- 要编码的字符串(通常为一个URI)

encodeURIComponent函数返回值

str编码后的字符串(原str中的某些字符被十六进制的转义序列替换)

encodeURIComponent函数说明

encodeURIComponent是全局函数。encodeURIComponent的目的是给URI进行编码。与encodeURI函数的不同encodeURIComponent会对URI中具有特殊意义的字符也进行编码; ; / ? : @ & = + $ , # 空格,其它的与encodeURI相同。

示例

var str="梦之都 http://www.dreamdu.com/";
str=encodeURIComponent(str);
document.write(str);
str=decodeURIComponent(str);
document.write(str);
str=encodeURI(";/?:@&=+$,#-_.!~*'()");
document.write(str);
str=encodeURIComponent(";/?:@&=+$,#-_.!~*'()");
document.write(str);

结果:

%E6%A2%A6%E4%B9%8B%E9%83%BD%20http%3A%2F%2Fwww.dreamdu.com%2F
梦之都 http://www.dreamdu.com/
;/?:@&=+$,#-_.!~*'()
%3B%2F%3F%3A%40%26%3D%2B%24%2C%23-_.!~*'()

JavaScript encodeURIComponent() 函数示例 -- 可以尝试编辑

decodeURI函数异常

URIError -- str中含有格式化错误的Unicode字符,无法编码

延伸阅读



  • 教程中有什么不懂的地方?发现教程的错误!对教程有什么建议!快快给我留言呀 给猴子留言,我会尽快解答你的问题:)
  • HTML代码