现在的位置: 首页 > 综合 > 正文

JavaScript – document.write(String content) writes nothing on html page

2014年05月18日 ⁄ 综合 ⁄ 共 672字 ⁄ 字号 评论关闭

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 an
unready (open) document. That is to say, When the page finishes loading, the document becomes
closed. An attempt to document.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>

抱歉!评论已关闭.