JavaScript - document.write(String contect) writes nothing on html page

The following html will output nothing on html page, you may meet it before.

<html>
<head>
    <title>Tmp HTML</title>
    <script type="text/javascript">
        document.write(String.fromCharCode(0x204A));
    </script>
</head>
<body>
</body>
</html>

Why nothing displayed? It is because bothdocument.write anddocument.writeln method output text into anunready (open) document. That is to say, When the page finishes loading, the document becomesclosed. An attempt todocument.write in it will cause the contents to be erased.

You can work around as following:

<html>
<head>
    <title>Tmp HTML</title>
    <script type="text/javascript">
        function MyDocumentWrite() {
            document.write(String.fromCharCode(0x204A));
        }
        window.onload = MyDocumentWrite;
    </script>
</head>
<body>
</body>
</html>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。