<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function doCode(deCode)
{
var txt1 = document.getElementById("txt1");
var txt2 = document.getElementById("txt2");
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(deCode)
{
txt2.value = urlDecode(txt1.value);
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
txt2.value = urlEncode(txt1.value);
}
}
function urlDecode(str)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if("undefined" == typeof decodeURIComponent)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
return unescape(str).replace(/\+/g, ' ').replace(/%2B/g,'+');
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*just use my self decode script*/
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*return decodeURIComponent(str.replace(/\+/g, ' ').replace(/%2B/g,'+'));*/
return unescape(str).replace(/\+/g, ' ').replace(/%2B/g,'+');
}
}
function urlEncode(str)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var i,c,ret="",strSpecial="!\"#$%&'()*+,/:;<=>?@[\]^`
{|}~%";
for(i=0;i<str.length;i++)
{
c=str.charAt(i);
if(c==" ")
ret+="+";
else if(strSpecial.indexOf(c)!=-1)
ret+="%"+str.charCodeAt(i).toString(16);
else
ret+=c;
}
return ret;
};
</script>
</head>
<body>
<div id="output">
Source:<br />
<textarea name="txt1" cols="52" rows="12"></textarea><br />
Destination:<br />
<textarea name="txt2" cols="52" rows="12"></textarea><br />
<input name="Decode" type="button" value="Decode" οnclick="doCode(true);" />
<input name="Encode" type="button" value="Encode" οnclick="doCode(false);" />
</div>
</body>
</html>
转载于:https://www.cnblogs.com/afxcn/archive/2007/04/24/725916.html