In JavaScript, document. write is used to output content to the page.

Source: Internet
Author: User

In JavaScript, document. write is used to output content to the page.

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:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Document. write ("I am the text output to the page! ");
</Script>

Save the preceding code as write.html (or write.htm) in a text editor ). Double-click write.html to run it (the file is opened in IE or another browser). The following text is displayed on the page:

Copy codeThe Code is as follows: I am the text output to the page!

Prompt

If you use the IE browser, the following message is displayed: "to protect security, Internet Explorer has restricted this webpage from running scripts or ActiveX spaces that can access your computer .", Select "blocked content" to allow IE to run the above JavaScript code.

Document. write outputs text with html tags to the page

If the text output to the page contains html tags, you only need to add tags to the output text:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Document. write ("</Script>

Instance 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 character to be output is caused by "" or ''and placed in document. write.

The document. write command and the pop-up box command alert used in this example will be frequently used in future JavaScript learning.


The document output by using documentwrite in javascript directly clears the content on the current page. How can this problem be solved?

Assume that you use document. write in this way.
Document. write (str );
Then you can rewrite it
Document. body. innerHTML + = str;
In this way, the output will be made after the original webpage.
Below is my test code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Script>
Function writeit ()
{
Document. body. innerHTML + = "fuck me behind ";
}
</Script>
</Head>

<Body>
<A href = "javascript: writeit ();"> write </a>
</Body>
</Html>

Why can't the content in documentwrite in JavaScript be displayed on the page? Guidance

After the Generally, document. write is used to output data by spelling html before html documents are output.
We recommend that you use div for your situation.
<Html>
<Head>

</Head>
<Body>
<H1> guess number <Hr>
<P> I secretly selected a number ranging from 1 to 100. Can you enter the number? Note: You can guess a maximum of 10 times! </P>
<Form name = "form" action = "number.htm" onSubmit = "return check ()">
<Input type = "text" name = "number">
<Input type = "submit" value = "right? ">
</Form>
<Script type = "text/javascript">
Function writeconsystemic (str ){
Var div = document. createElement ('div ');
Div. innerHTML = str;
Document. body. appendChild (str );
}
Function check (){
Var realResult = 70;
Var guessNumber = document. form. number. value;
If (guessNumber> realResult ){
Writeconsystemic ("Try again! ");
Document. form. number. focus ();
Return false;
}
If (guessNumber <realResult ){
Writeconsystemic ("Try again! ");
Document. test. number. focus ();
Return false;
}
If (guessNumber = realResult ){
Alert ("You guessed it! ");
}
Return true;
}
</Script>

</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.