How to Write Text in JavaScript
JavaScript allows you to dynamically write text onto the page, using the
document.write and document.writeln functions.Instructions
-
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 closingtag. -
-
3
Write text into a document in JavaScript using the
document.writeordocument.writelnsyntax. 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")
ordocument.writeln("My Text")
-
1
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.