web前端url传递值 js加密解密

一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:javascript

1.传参页面
Javascript代码:html

<script type=”text/javascript”>// <![CDATA[
function send(){
var url = "test01.html";
var userName = $("#userName").html();
window.open(encodeURI(url + "?userName=" + userName)); }
// ]]>
</script>

2. 接收参数页面:test02.htmljava

<script>
var urlinfo = window.location.href;//獲取url
var userName = urlinfo.split(“?”)[1].split(“=”)[1];//拆分url获得”=”後面的參數
$(“#userName”).html(decodeURI(userName));
</script>

 

注意:在编码时要两次编码编码

<script type="text/javascript">
    window.location.href = "Index.aspx?value=" + encodeURIComponent(encodeURIComponent("中文编码"));
</script>

 

 

二:如何获取Url“?”后,“=”的参数值:url

A.首先用window.location.href获取到所有url值。
B.用split截取“?”后的所有
C.split(“?”)后面的[1]内数字,默认从0开始计算spa

三:Js中escape,unescape,encodeURI,encodeURIComponent区别:code

1.传递参数时候使用,encodeURIComponent不然url中很容易被”#”,”?”,”&”等敏感符号隔断。
2.url跳转时候使用,编码用encodeURI,解码用decodeURI。
3.escape() 只是为0-255之外 ASCII字符 作转换工做,转换成的 %u**** 这样的码,若是要用更多的字符如 UTF-8字符库 就必定要用 encodeURIComponent() 或 encodeURI() 转换才能够成 %nn%nn 这的码才能够,其它状况下escape,encodeURI,encodeURIComponent编码结果相同,因此为了全球的统一化进程,在用 encodeURIComponent() 或 encodeURI() 代替 escape() 使用吧!htm

相关文章
相关标签/搜索