`

js实现urlencode和urldecode

js 
阅读更多

最近发现写往cookie里面写值读出来是经过URLEncode过的。所以需要用js解码.所以用js实现了urlencode和urldecode
var littleUrl ={
		// public method for url encoding
		encode : function (string){
			return escape(this._utf8_encode(string));
		}
		,
		// public method for url decoding
		decode : function (string){
			return this._utf8_decode(unescape(string));
		}
		,
		// private method for UTF-8 encoding
		_utf8_encode : function (string){
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
			for (var n = 0; n < string.length; n++){
				var c = string.charCodeAt(n);
				if (c < 128){
					utftext += String.fromCharCode(c);
				}
				else
				if((c > 127) && (c < 2048)){
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else{
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
			}
			return utftext;
		}
		,
		// private method for UTF-8 decoding
		_utf8_decode : function (utftext){
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
			while ( i < utftext.length ){
				c = utftext.charCodeAt(i);
				if (c < 128){
					string += String.fromCharCode(c);
					i++;
				}
				else
				if((c > 191) && (c < 224)){
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else{
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}
	}
 
分享到:
评论

相关推荐

    用JavaScript实现PHP的urldecode/urldecode函数

    这个是 utf-8版本的 js实现 php的 urlencode() 和 urldecode()两个函数的功能。 在传送cookie的时候 在 php端实现 url编码 但要用 js来解码cookie的时候 就出现汉字不能不能识别的问题 这个 js 很好的解决

    用JavaScript实现UrlEncode和UrlDecode的脚本代码

    用js自定义函数写的实现url加密解密的实现代码,需要的朋友可以参考下

    用JavaScript实现PHP的urlencode与urldecode函数

    主要介绍了用JavaScript实现PHP的urlencode与urldecode函数,很多情况下我们用了出来php urlencode出来的网址,需要的朋友可以参考下

    JS版 UrlEncode

    JS版 UrlEncode 和 UrlDecode 函数

    Python3中urlencode和urldecode的用法详解

    在Python3中,将中文进行urlencode编码使用函数 urllib.parse.quote(string, safe='/', encoding=None, errors=None) 而将编码后的字符串转为中文,则使用 urllib.parse.unquote(string, encoding='utf-8', errors...

    php自定义urlencode,urldecode函数实例

    本文实例讲述了php自定义urlencode,urldecode函数。分享给大家供大家参考。具体如下: //配合JavaScript的ajaxObject函数, 对字串进行转码. function ajax_encode($str){ $patern = array("/%/","/=/","/&/"); /...

    js实现urlencode加密适用于ut8编码

    在网站开发过程中,使用汉字传输会导致有时URL会出现乱码的问题,可通过urlencode对中文进行编码,然后再解码,避免出现乱码,此函数加密的字符串可通过php的urldecode进行解码

    Javascript UrlDecode函数代码

    将Url进行编码,前台JS需要使用这段内容,这时候就需要解码了: 代码 代码如下: /** * Url编码 **/ ShengUtils.encode = function(unzipStr){ var zipstr=””; var strSpecial=”!\”#$%&'()*+,/:;&lt;=&gt;?[]^`{|}~%”...

    Javascript下的urlencode编码解码方法附decodeURIComponent

    而相信碰到过此问题的朋友应该都有所了解,目前网络上流行一些js下的自定义函数去解决这个问题,如说vbscript(URLDecode())、javascript(UrlDecode())等。而这两个函数,都无法很好的与asp(Server.Ur

    js中escape对应的C#解码函数 UrlDecode

    js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码 需要注意的几点: 1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。...

    java net unicode / native2ascii / url decode / url encode / UTF8 / js url code

    NULL 博文链接:https://lindows.iteye.com/blog/259125

    js 中文gbk编码

    ** 功能:URLEncode,URLDecode ** 示例: --------------------------------------------------------------------------------------------------- 用法:参见组件地址 ---------------------------------...

    JavaScript的URL encode decode源代码,ip验证,TRim

    NULL 博文链接:https://icecream0.iteye.com/blog/854682

    各种加密解密在线演示

    文字在线加密解密、散列/哈希、BASE64、SHA1、SHA224、SHA256、SHA384、SHA512、MD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512、HmacMD5、urlencode、urldecode

    chrome开发工具箱插件

    二维码生成,网页抓取,MD5加密(16位、32位),UrlEncode/UrlDecode,Base64Encode/Base64Decode,CSS/JS/Html代码美化、压缩,人民币大小写转换,简体繁体转换,ASCII码转换,颜色选择器,EsCape/UnEscape,8、10...

    url 编码 js url传参中文乱码解决方案

    1.配置文件web.config中 在节中加上整个网站的编码方式。 ”GB2312″ requestEncoding=”GB2312″ responseEncoding=”GB2312″/&gt; 这样参数就以gb2312的... 然后在接收页解码: string mm = Server.URLDecode(Requext

    慧聪网快捷助理-crx插件

    慧聪网多帐号快速切换 全自动一键重发商机(未开放) 全自动商机询盘应答(未开放) ……开发工具- 快速清除浏览器缓存 MD5加密(16位、32位) UrlEncode/UrlDecode Base64Encode/Base64Decode CSS/JS/Html代码美化...

Global site tag (gtag.js) - Google Analytics