Home >Web Front-end >JS Tutorial >Strange problems encountered when using document.write in web pages_javascript skills
During the research on porting the Razor template engine to JavaScript a few days ago, I found that if the content output using document.write contains the <script> tag that calls external JavaScript, there may be a problem - here The external JavaScript called inside may be executed at inappropriate times. For IE and Opera, these external JavaScript will not be executed until all other parts of the write parameters have been parsed. <br><br>In further research, I found that for Chrome and Safari, two browsers that use the WebKit web page layout engine, if the externally called JavaScript calls document.write again to write another If the <script> tag of external JavaScript is called, the external JavaScript called for the second time will not be executed, and some subsequent content will also be disrupted - the actual situation is actually a little more complicated, and it is different from the second and third levels. It is related to the specific content of the parameters in the document.write call. Due to time and energy constraints, I am unable to conduct more specific tests. <br><br>After these studies, the only surviving among the major mainstream browsers that can correctly handle all document.write calls is Firefox. <BR>Firefox’s pseudo “multi-threaded” event processing feature has always been something that I dislike very much. This incident can be regarded as a proof of “each has its own strengths”. <br><br>To understand the various phenomena caused by this problem, you can download write-test.zip, unzip it and open the write-test.htm in it with various browsers. <br><br>In the past few days, I have been trying to write a replacement function to solve this problem, and it has successfully passed the test in the link above. <BR>If I don’t find any obvious problems in the next few days, I will post it on the blog for everyone’s reference. <BR><A href="http://xiazai.jb51.net/201008/yuanma/write-test.rar" target=_blank>Test code, package and download</script>