JavaScript document.write() method

 JavaScript document.write is one of the functions in the Document object collection. Through document.write, you can easily write the string content where you want to display text on the web page. When you are writing HTML , To display a paragraph of text on a web page, the easiest way is to write the string directly in HTML . In fact, you can also use the document.write() method of JavaScript to do it. Not only a string, but also contains HTML tags, such as wrapping or displaying images.


JavaScript document.write() basic syntax

document.write('Write the text you want to present here');

Place the text content to be presented in document.write parentheses, and use single quotes (") or double quotes ("") to wrap the string so that the browser will automatically present the text as a string. If you The text content to be presented is a variable, so it cannot be directly placed in quotation marks. A comma (,) or plus sign (+) must be used to connect the variable number string. When there are many variables, it is recommended to use the plus sign (+). Concatenate the variable number string to avoid display errors caused by the browser's judgment error.

JavaScript document.write example 1. Simple display string
<script type="text/javascript">
 document.write("This is the test page content");
</script>
The above example presents the result as
This is the test page content
JavaScript document.write example two, adding HTML tags and variable number string
<script type="text/javascript">
 var TestString='Have a good time.';
 document.write('Welcome to Wibibi.<br>'+TestString);
</script>
The above example presents the result as
Welcome to Wibibi.
Have a good time.
In the second example, the work content of document.write includes outputting a simple string and outputting a variable number string. In the string in the previous paragraph, the HTML line break tag <br> is used, followed by a plus sign (+). The variable number string TestString will output the first string and the next string together, and the output result is divided into two lines. Pay special attention to the variable number string cannot be placed in quotation marks, single or double quotation marks are not allowed, otherwise it will become pure The text string will not output the variable content.

Post a Comment

0 Comments