How to solve the encoding problem caused by dynamically creating scripts to cache js files in IE _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to solve the encoding problem caused by dynamically creating scripts to cache js files in IE. If you need a script, refer to the following to see how to reproduce the code first.

1. gb2312.html the file is encoded as gb2312

The Code is as follows:










Test
Script
Function loadJS (src, charset ){
Var script = document. createElement ('script ');
Script. src = src;
Script. charset = charset;
Var head = document. getElementsByTagName ('head') [0];
Head. appendChild (script );
}
Script



2. utf8.js the file is encoded as UTF-8

The Code is as follows:


Var p = document. createElement ('P ');
P. innerHTML = 'ie cache causes gibberish ';
Document. body. appendChild (p );


The loadJS function dynamically creates a script, sets src, charset, and adds it to the head. Every time you click the button here, utf8.js will be introduced to this page. The code in utf. js will create a p element to set a piece of text and then add it to the body.

When you click the button for the first time, the text is displayed normally.

The second time later, the text encoding is incorrect.





If the script tag is not dynamically created and is directly written on the html page, no such problem occurs.

The Code is as follows:


Related Article

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.