How to Print in Javascript

Опубликовал Admin
16-09-2020, 09:40
4
0
In computer programming, Print is a command used to display some type of text on-screen. It can be used to display text (string), a variable, numbers or math calculations. One of the first things many programmers learn is how to use a Print command to create a program that displays "Hello World!". Unlike other programming languages, jаvascript is rendered within a web browser. jаvascript doesn't have a simple Print command like a lot of other languages do. However, there are many ways you can display text either within the HTML document, an alert window, the console. You can also use jаvascript to print from a printer.

Using InnerHTML

  1. Type <p id= in the body of your HTML document. This is the opening HTML tag for paragraph text with a specific id.
    • For more information on how to write HTML code, check out How to Create a Simple Web Page with HTML.
  2. Type a name for the paragraph element in quotes followed by >. This assigns the paragraph text element an ID that can be recalled in jаvascript. For example, "<p id="math">".
    • You can also use other text HTML elements, such a header {{kbd|<h1 id=>
  3. WH.shared.addScrollLoadItem('fe886b6f56848a9bba433f18c429c7c2') {"smallUrl":"https:\/\/www.wikihow.com\/images\/thumb\/5\/56\/Print-in-jаvascript-Step-3.jpg\/v4-460px-Print-in-jаvascript-Step-3.jpg","bigUrl":"\/images\/thumb\/5\/56\/Print-in-jаvascript-Step-3.jpg\/aid7174452-v4-728px-Print-in-jаvascript-Step-3.jpg","smallWidth":460,"smallHeight":344,"bigWidth":"728","bigHeight":"545","licensing":"<div class=\"mw-parser-output\"><p>License: <a target=\"_blank\" rel=\"nofollow noreferrer noopener\" class=\"external text\" href=\"https:\/\/en.wikipedia.org\/wiki\/Fair_use\">Fair Use<\/a> (screenshot)<br>\n<\/p><\/div>"}

Using a Window Alert

  1. Type <script> in the body of your HTML document. This is the opening tag for your jаvascript.
  2. Press ↵ Enter and type alert. This is the jаvascript command to open a pop-up window alert.
  3. Type what you want to print in parenthesis. You can type text, a variable, numbers, or a math equation. For example, "alert("Hello Sweety!")" to display "Hello Sweety!" in a pop-up window.
  4. Add a semi-colon (;) at the end. This closes your line of jаvascript. The entire line should look something like "alert("Hello Sweety!");".
  5. Press ↵ Enter and type </script>. This tag closes your jаvascript code. Type this tag when you are finished entering all your lines of jаvascript. When you load your HTML document in a web browser, it will display the text you want to print in a pop-up window.
    • You can also create a button that displays printed text in a pop-up window. To do so, type "<button type="button" onclick="alert([text you want to display])">Button text</button>".

Using Console Log

  1. Type <script> in the body of your HTML document. This is the opening tag for your jаvascript.
  2. Press ↵ Enter and type console.log. This is the jаvascript command for a console log. A console log is the closest thing to a Print command in the typical coding sense. It doesn't display text on your web page, but it does display text inside the console when you run your HTML/jаvascript inside a web browser. This is useful for debugging.
  3. Type the text you want to log in parenthesis. It can be text (a string), a variable, numbers, or a math equation. For example, "console.log(I am printing in jаvascript).
  4. Add a semi-colon (;) at the end. This closes your line of jаvascript. The entire line should look something like "nowiki>console.log(I am printing in jаvascript)</nowiki>
  5. Press ↵ Enter and type </script>. This tag closes your jаvascript code. Type this tag when you are finished entering all your lines of jаvascript.
  6. Save your HTML document. Use the following steps to save your HTML document.
    • Click File at the top of your text editor or HTML editing program.
    • Click Save as.
    • Type a file name for the HTML document.
    • Ensure it saves as a ".html" document.
    • Click Save.
  7. Open the HTML document in a web browser. Open any web browser of your choice and type the file location of the HTML document in the address bar at the top. This will open the HTML document in your web browser.
  8. Press F12. This opens Developer Mode in your web browser. You will see a new window with the HTML, CSS, and jаvascript code displayed.
  9. Click Console. It's at the top of the developer mode window.
  10. Refresh your browser . It's the icon that resembles a circular arrow at the top of your web browser. This reloads the HTML document in the web browser. As the web browser processes the HTML and jаvascript, it will display the console log text inside the console in the Developer Mode window.

Printing from a Printer

  1. Type <button onclick= in the body of your HTML document. This is the opening HTML tag to create a button within your HTML.
  2. Type "window.print()"> after the equals (=) sign. The "window.print()" command is the jаvascript command to print the contents of your web browser window. The bracket (>) finishes the opening tag of the HTML button.
  3. Type the text you want to go in the button. This should go right after the bracket (>). For example, you could type "Click here to print" or something similar.
  4. Type </button>. This closes out the HTML button element. When you render your HTML in a web browser, it will create a button you can click to print the contents of yoru web browser.

Tips

Теги:
Information
Users of Guests are not allowed to comment this publication.