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: