Solve the Problem of garbled code when passing parameters to the GBK page in js UTF-8

Source: Internet
Author: User

Js UTF-8 to pass the Chinese parameters to GBK, the reason is not because the character length is different, 1 Chinese in the UTF-8 for 3 characters, while GBK is 2 characters, therefore, the encoding content after encodeURL is inconsistent.

To convert the encoding, you cannot do without the server. Therefore, the best way is to create a transit page and you can prompt "Jump to xxx". However, the essence of this page is to handle Chinese parameter problems, use js to pass parameters to the transit page through URl, and use $ _ GET to determine whether a parameter exists. If yes, use iconv to convert and encode, and then combine the converted URL parameters, go to the GBK page.

The following is a simple job transfer by the author for your reference.

The Code is as follows:  
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/5o/xhtml" xml: lang =" zh-CN "lang =" zh-CN ">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/>
</Head>
<Body>
<H1> redirecting to the corresponding server ........ </H1>

<? Php
$ Url = array (
'Tmall' => 'HTTP: // s8.taobao.com/search? Commend = all & style = grid & pid = mm_11039324_251_38_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. After you click OK, the system will automatically jump back to the Homepage"); 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>
</Html>

Processing of JS file encoding with UTF-8 page reference external code as GBK

For example, for qq ip return city interface js we use the following code, but in the UTF-8 page normal display GBK encoding js

The Code is as follows:  

<Script type = "text/javascript" src = "http://f?qq.com/ipaddres" charset =" gb2312 "> </script>

In this way, I successfully solved the problem of introducing garbled JavaScript files encoded as GBK IN THE UTF-8 page. And do not need additional Write Functions to handle GBK to UTF-8 encoding, also for GBK page reference encoding for the UTF-8 encoding of JavaScript files if garbled problem, you can use the following code to solve

The Code is as follows:  

<Script type = "text/javascript" src = "test. js" charset = "UTF-8"> </script>

This method is for the js file appears in Chinese characters, and other easily caused in GBK and UTF-8 Encoding Error characters when using, for pure English do not need. It's okay if you want to protect your security ~

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.