---
title: "jsURL编码"
author: "accpxudajian"
date: "2012-11-27T11:03:27Z"
tags: []
categories: ["移动开发"]
slug: "jsurl"
draft: false
lastmod: "2012-11-27T11:03:27Z"
id: 1735825
url: "https://angrycoder.iteye.com/blog/1735825"
---

js 对文字进行编码涉及3个函数：**escape,encodeURI,encodeURIComponent**，相应3个解码函数：unescape,decodeURI,decodeURIComponent

1、   传递参数时需要使用encodeURIComponent，这样组合的url才不会被#等特殊字符截断。                            

例如：&lt;script language="javascript"&gt;document.write('&lt;a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'"&gt;退出&lt;/a& gt;');&lt;/script&gt;

**2、   进行url跳转时可以整体使用encodeURI（最常用的）**

例如：Location.href="/encodeURI"("http://cang.baidu.com/do/s?word=百度&ct=21");

3、   js使用数据时可以使用escape

例如：搜藏中history纪录。

 

 

原文地址：[http://www.blogjava.net/freeman1984/archive/2010/06/07/322965.html](http://www.blogjava.net/freeman1984/archive/2010/06/07/322965.html)

 

 

愤怒的coder - 编码很重要。