Solve JS UTF-8 to GBK page garbled problem

Source: Internet
Author: User

JS UTF-8 pass Chinese parameters to GBK, the reason is not because of its character length is different, 1 Chinese in UTF-8 for 3 characters, and GBK 2 characters, so that the different coding content after encodeurl inconsistent.

To convert the encoding, how can not be separated from the server, so the best way is to do a transit page, you can hint "is jumping to xxx", but the essence of this page is to deal with the Chinese parameter problem, through JS, with the URL to transfer the page, the transfer page again through $_get to determine whether there are parameters, There is the use of Iconv conversion code, and then combine the conversion of the URL parameters, jump to the GBK page.

The following is the author of a simple transfer, for students who do not understand the reference

The code is as follows Copy Code
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/>
<body>

<?php
$url = Array (
' Tmall ' => ' http://s8.taobao.com/search?commend=all&style=grid&pid=mm_11039324_2536738_9491366& Mode=66&viewindex=10&user_type=1&q= ',
' 360buy ' => ' Http://search.360buy.com/Search?utm_source=show.union.360buy.com&utm_medium=tuiguang&utm _campaign=t_19195_&keyword= ',
);
if ($_get[' url '] = = "&& $_get[' q ' = =")
Echo ' <script>alert ("The parameter is incorrect, the point will automatically jump back to the page"); window.location.href= "http://', $_server[' http_host '", "</ Script> ';
Else
{
Echo ' <script>window.location.href= ', $url [$_get[' url ']],iconv (' utf-8′, ' GBK ', $_get[' Q ']), ' </script > ';
}

?>

</body>


UTF-8 page refers to the processing of JS file encoding with external encoding as GBK

For example, for QQ IP return to the interface of the city of JS we use the following code, but in the UTF-8 page normal display GBK encoded JS

The code is as follows Copy Code

<script type= "Text/javascript" src= http://fw.qq.com/ipaddres "charset=" gb2312″></script>

That's how I managed to solve it. In the UTF-8 page, the introduction of coded GBK JavaScript file garbled problem. And do not need additional write function processing GBK transcoding UTF-8 encoding, also for GBK page reference encoding for UTF-8 encoded JavaScript file If garbled problem, you can use the following code to solve

The code is as follows Copy Code

<script type= "Text/javascript" src= test.js "charset=" utf-8″></script>

This method is for the Chinese characters in the JS file, as well as other easy to cause in GBK and UTF-8 encoding error characters, for pure English will not need. If it's for insurance, plus it's OK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.