In this post I'm going to discuss about Javascript outputs. javascript doesn't have any built in print or display functions.
javascript display possibilities
javascript can "display" data in different ways:
using window.alert()
you can use an alert box display data:
using document.write()
for testing purpose, it is convenient to use document.write():
using document.write() after an HTML document is fully loaded, will delete all existing HTML.
the document.write() method should be used only for testing
using innerHTML
to access an HTML element, javascript can use the document.getElementById(id) method.
The id attribute defines thee HTML element. The innerHTML property defines the HTML content:
to "display data" in HTML, (in most cases) you will set the value of an innerHTML property.
using console.log()
in your browser, you can use the console.log() method to display data.
Activate the browser console with F12 and select "console" in the menu.
javascript display possibilities
javascript can "display" data in different ways:
- writing into an alert box, using window.alert().
- writing into the HTML output using document.write()
- writing into an HTML element, using innerHTML.
- writing into the browser console, using console.log().
using window.alert()
you can use an alert box display data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> <html> <head> </head> <body> <h1>My first web page</h1> <p>my first paragraph.</p> <script> window.alert(5 + 6); </script> </body> </html> |
using document.write()
for testing purpose, it is convenient to use document.write():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> <html> <head> </head> <body> <h1>My first web page</h1> <p>my first paragraph.</p> <script> document.write(5+6) </script> </body> </html> |
using document.write() after an HTML document is fully loaded, will delete all existing HTML.
1 2 3 4 5 6 7 8 9 10 11 12 | <!DOCTYPE html> <html> <head> </head> <body> <h1>My first web page</h1> <p>my first paragraph.</p> <button onclick="document.write(5+6)">Try it</button> </body> </html> |
the document.write() method should be used only for testing
using innerHTML
to access an HTML element, javascript can use the document.getElementById(id) method.
The id attribute defines thee HTML element. The innerHTML property defines the HTML content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html> <html> <head> </head> <body> <h1>My first web page</h1> <p>my first paragraph.</p> <p id="demo"></p> <script> document.getElementById('demo').innerHTML = 5 + 6; </script> </body> </html> |
to "display data" in HTML, (in most cases) you will set the value of an innerHTML property.
using console.log()
in your browser, you can use the console.log() method to display data.
Activate the browser console with F12 and select "console" in the menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html> <html> <head> </head> <body> <h1>My first web page</h1> <p>my first paragraph.</p> <p id="demo"></p> <script> console.log(5 + 6); </script> </body> </html> |

Comments
Post a Comment