网址编码函数

 

我们知道一个网址 自己的网址,   不同页面也有自己id网址, 我们经常会做一些, 把网址送入到后台。  但是后台再处理的 不认识比如 换行啊 等特殊符号的  ?  

var url =  “http://www.itast.cn?name=cz”  

所以我们要实现编码,然后再传到后台。

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码

decodeURIComponent() 函数可把字符串作为 URI 组件进行解码

var url = "http://www.itcast.cn?name=andy";
console.log(encodeURIComponent(url));  // 编码
var afterUrl = encodeURIComponent(url);
console.log(decodeURIComponent(afterUrl));  // 解码

结果:

posted @ 2019-12-25 17:26  杨小越  阅读(145)  评论(0)    收藏  举报