javascript中文url乱码怎么办


本文摘自PHP中文网,作者醉折花枝作酒筹,侵删。

针对中文乱码问题,最主要是通过(encodeURI,decodeURI),(encodeURIComponent,decodeURIComponent)两种方法进行参数的编码以及解码工作,其中前者最主要针对的是整个url参数。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

在日常开发当中,我们可能遇到要将某个页面的参数通过url链接拼接的方式传递到另一个页面当中,在另一个页面当中进行使用,如果传输过去的是中文,那么可能会遇到中文乱码问题,那么该如何来解决呢?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!--test01.html-->

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>

</head>

<body>

 

<p id="userName">你好明天</p>

 

<p οnclick="send();">点击测试</p>

 

<script>

    function send(){

        var url = "test02.html";

        var userName = $("#userName").html();

//        window.open(encodeURI(url + "?userName=" + userName));     //encodeURI针对整个参数进行编码

        window.open(url + "?userName=" + encodeURIComponent(userName));  //encodeURIComponent针对单个参数进行编码

 

    }

</script>

 

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<!--test02-->

 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Title</title>

    <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>

</head>

 

<body>

 

<p id="userName"></p>

 

<script>

    var urlinfo = window.location.href;//获取url

    var userName = urlinfo.split("?")[1].split("=")[1];//拆分url得到”=”后面的参数

//    $("#userName").html(decodeURI(userName));          //decodeURI针对整个参数进行解码

    $("#userName").html(decodeURIComponent(userName));   //decodeURIComponent针对单个参数进行解码

//    $("#userName").html(userName);

</script>

 

</body>

</html>

针对中文乱码问题,最主要是通过(encodeURI,decodeURI),(encodeURIComponent,decodeURIComponent)两种方法进行参数的编码以及解码工作,其中xxxxURI最主要针对的是整个url参数,xxxxURIComponent针对的是单个url参数;

简单的分享就到这里,如有疑问,欢迎留言~

【推荐学习:javascript高级教程

以上就是javascript中文url乱码怎么办的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript中文url乱码怎么办

javascript如何解决url中文乱码问题

更多相关阅读请进入《url中文乱码》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...