第一部分:html
<input type="button" value="编码" class="sp-btn-blue" οnclick="urlEncode()" />
<input type="button" value="解码" class="sp-btn-blue" οnclick="UrlSearch()" /><label id="showName"></label>
第二:js部分
<script type="text/javascript">
function urlEncode() {
var UserName_New="小网";
var encodename=encodeURI(UserName_New);
console.log(encodename);
window.location.href = "http://localhost:2510/encodeChinese.html?UserName=" + encodename+"&haode=121";
//byte[] UserName_bytes = System.Text.Encoding.UTF8.GetBytes(UserName_New); //将字符串转为bytes
//var UserName = HttpUtility.UrlEncode(UserName_bytes);
}
function getUrl() {
var str = location.href; //取得整个地址栏
var num = str.indexOf("?")
str = str.substr(num + 1); //取得所有参数
var loastr = "";
var loc = "";
$.each(str.split('&'), function (index, item) {
item = item.split('=');
var name = item[0];
var val = item[1];
loastr = "\"" + name + "\":\"" + val + "\",";
loc += loastr;
});
loc = loc.substring(0, loc.length - 1);
loc = "{" + loc + "}";
var obj = JSON.parse(loc);//将字符型数组转换为对象
return obj;
}
function UrlSearch() {
var geturltext = getUrl(); //得到全部参数
var getName = decodeURI(geturltext.UserName);
//alert(getName);
$("#showName").text("显示名称:"+getName);
}
</script>