zoukankan      html  css  js  c++  java
  • 一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码:

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

    1.传参页面
    Javascript代码:<script type=”text/javascript”>// <![CDATA[
    function send(){
    var url = "test01.html";
    var userName = $("#userName").html();
    window.open(encodeURI(url + "?userName=" + userName)); }
    // ]]>
    </script>

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

    <script>
    var urlinfo = window.location.href;//獲取url
    var userName = urlinfo.split(“?”)[1].split(“=”)[1];//拆分url得到”=”後面的參數
    $(“#userName”).html(decodeURI(userName));
    </script>
    二:如何获取Url“?”后,“=”的参数值:

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

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

    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() 使用吧!

    可以任意转载, 转载时请务必以超链接形式标明文章原始出处及此声明

    本文地址: http://www.weiking.com.cn/post/654.html

  • 相关阅读:
    Nginx负载均衡+代理+ssl+压力测试
    Nginx配置文件详解
    HDU ACM 1690 Bus System (SPFA)
    HDU ACM 1224 Free DIY Tour (SPFA)
    HDU ACM 1869 六度分离(Floyd)
    HDU ACM 2066 一个人的旅行
    HDU ACM 3790 最短路径问题
    HDU ACM 1879 继续畅通工程
    HDU ACM 1856 More is better(并查集)
    HDU ACM 1325 / POJ 1308 Is It A Tree?
  • 原文地址:https://www.cnblogs.com/weiying/p/weiying_09_19.html
Copyright © 2011-2022 走看看