Four ways to Output Javascript

Source: Internet
Author: User
Tags button type

JavaScript output
JavaScript does not have any print or output functions
Data can be output in different ways

Use the Window.alert () pop-up warning box
Use the document.write () method to write content to an HTML document
Use document.getElementById (""). Innerhtml= "value to write"; Writing to HTML elements
Use Console.log () to write to the console of the rover


1: Use Window.alert ()
<script>
Window.alert (' warning box ');
</script>

2: Manipulating HTML elements
If you want JavaScript to access an HTML element, you can use the document.getElementById (ID) method
Use the id attribute to identify the HTML element and innerHTML to get or insert the element content:


<p id= "Demo" > my first paragraph </p>
<script>
document.getElementById ("Demo"). Innerhtml= "paragraph modified"
</scirpt>
document.getElementById ("Demo") is the JavaScript code that uses the id attribute to find HTML elements. The method is defined in the HTML DOM.
InnerHTML = "Paragraph changed." Is the JavaScript code used to modify the HTML content (InnerHTML) of an element.

3: Write to HTML document
Use the document.write () method to write content to HTML content, which can be written directly to the HTML document with content or HTML tags
<script>
Docuemnt.write (Date ());//date () Output China standard Time
</script>

Note: If you execute document.write () after the document has finished loading, the entire HTML page will be overwritten
Sample:
<button type= "button" onclick= "MyFunction" > Buttons </buton>
<script>
function MyFunction () {
document.write (Date ());
}
</scipt>


4: Write to console

If your browser supports debugging, you can use the Console.log () method to display JavaScript values in the browser.
In the browser, use F12 to enable debug mode and click the "Console" menu in the Debug window.

<! DOCTYPE html>
<body>
<script>
A = 5;
b = 6;
c = a + B;
Console.log (c);
</script>
</body>

Attention:

The Console.log () method allows you to see the output from your page, making it easier to debug JavaScript, and the console doesn't interrupt your page when compared to alert, and the console contains a lot of content, and you can enter console in the console.

Four ways to Output Javascript

Related Article

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.