The document.write command outputs text to the page
This example uses the document.write command of JavaScript to output the specified text to the page. The code is as follows:
The code copy is as follows:
<script type="text/javascript">
document.write("I am the text output to the page!");
</script>
Save part of the code above as write.html (or write.htm) using a text editor. Double-click write.html to run it (actually use IE and other browsers to open this file), and you will see the following text on the page:
Copy the code as follows: I am outputting text to the page!
hint
If using the IE browser prompts: "For security protection, Internet Explorer has restricted this webpage from running scripts or ActiveX space that can access the computer." Then select "Allow blocked content" to allow the IE browser to run the above JavaScript code.
document.write outputs text with html tag to the page
If the text output to the page is html tag, just include the tag in the output text:
The code copy is as follows:
<script type="text/javascript">
document.write("<h1>I am the text output to the page!</h1>");
</script>
Example syntax explanation
Similar to the previous "JavaScript pop-up prompt box example", document.write is a standard JavaScript syntax command used to output the specified string to the web page. The characters to be output are caused by "" or '' and placed in document.write().
The document.write command and pop-up command alert used in this example will be frequently used in future JavaScript learning, so be sure to use it proficiently.