Home  >  Article  >  Web Front-end  >  What to do if the javascript Chinese URL is garbled

What to do if the javascript Chinese URL is garbled

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-22 10:32:054466browse

To solve the problem of Chinese garbled characters, the most important thing is to encode and decode parameters through two methods: (encodeURI, decodeURI) and (encodeURIComponent, decodeURIComponent). The former is mainly aimed at the entire url parameter.

What to do if the javascript Chinese URL is garbled

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In daily development, we may encounter the need to pass the parameters of a certain page to another page through url link splicing, and use it in another page. If the transmission is in Chinese, Then you may encounter the problem of Chinese garbled characters, so how to solve it?

##

<!--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>
<!--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>

For the problem of Chinese garbled characters, the most important thing is to pass parameters through (encodeURI, decodeURI), (encodeURIComponent, decodeURIComponent) two methods The encoding and decoding work, among which xxxxURI mainly targets the entire url parameter, and xxxxURIComponent targets a single url parameter;

This is the end of the simple sharing. If you have any questions, please leave a message~

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of What to do if the javascript Chinese URL is garbled. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn