URL 编码
URL 只能使用 ASCII 字符集来通过因特网进行发送。由于 URL 常常会包含 ASCII 集合之外的字符,URL 必须转换为有效的 ASCII 格式。URL 编码使用 “%” 其后跟随两位的十六进制数来替换非 ASCII 字符。URL 不能包含空格。URL 编码通常使用 + 或者 %20 来替换空格。
URL 编码函数
JavaScript、PHP、ASP 都提供了对字符串进行 URL 编码的函数。
JavaScript 中使用 encodeURI() 函数,PHP 中使用 rawurlencode() 函数,ASP 中使用 Server.URLEncode() 函数。
JavaScript 转码代码
<html>
<head>
<script type="text/javascript">
function encode(){
document.getElementById("outputID").value = encodeURI(document.getElementById("inputID").value);
}
</script>
</head>
<body>
转码前:<input type="text" id="inputID" style="height:50px;width:1000px"/><br/><br/>
转码后:<input type="text" id="outputID" style="height:50px;width:1000px"/><br/><br/>
<Button onclick = "encode()" style="margin-left:200px;height:50px;width:500px;font-size:20px;font-weight:900">转码开始</Button>
</body>
</html>