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

  • 相关阅读:
    解决Flask使用pymysql驱动的Warning: (1366, "Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...'
    java中的抽象类
    java中的接口
    java中获取数组中的最大值
    java中的面向对象
    java中的数组
    java中的方法
    java中的流程控制结构
    java中的运算符
    java中的类型转换
  • 原文地址:https://www.cnblogs.com/weiying/p/weiying_09_19.html
Copyright © 2011-2022 走看看