Home > Web Front-end > JS Tutorial > body text

What to do if the javascript Chinese URL is garbled

醉折花枝作酒筹
Release: 2023-01-07 11:44:52
Original
4383 people have browsed it

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>
Copy after login
<!--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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!