How to Write Text in JavaScript

JavaScript allows you to dynamically write text onto the page, using the document.write and document.writeln functions.

Things You'll Need

  • JavaScript editor
Show More

Instructions

  1. Write Text in JavaScript

    • 1

      Decide what you want your JavaScript to display on your web page. JavaScript can write any arbitrary text, includnig the value of a variable or result of a computation.

    • 2

      Decide where and when you want your JavaScript to write to the browser. You can simply scan through the HTML code of your web page and insert the appropriate script wherever you want the text to appear. Placing the script right after the page's opening tag will display the text earlier than if the script is placed down by the page's closing tag.

    • 3

      Write text into a document in JavaScript using the document.write or document.writeln syntax. Both will write text to a browser. However the "document.writeln syntax" inserts a return after the text that it writes. Example:
      document.write("My Text")
      or
      document.writeln("My Text")

Tips & Warnings

  • Text can also be dynamically added to a web page by assigning the text to the value of existing elements, using JavaScript such as element.innerText="Invalid entry.";.

  • Don't forget to test your JavaScript in a variety of browsers before finalizing it, as some browsers interpret it differently than others.

Related Searches:

Comments

You May Also Like

Related Ads

Featured