| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Displaying Text in a Webpage (JavaScript) | Microsoft Docs |
01/18/2017 |
windows-client-threshold |
|
article |
|
|
3c2455e7-2402-45f2-9545-77a2b2490527 |
30 |
mikejo5000 |
mikejo |
ghogen |
Displaying Text in a Webpage (JavaScript)
There are a number of ways to display text in a webpage. Each has advantages and disadvantages and supports specific uses.
Displaying Text
-
The recommended way to display text is to create an element and write to its textContent property.
<div id="textDiv"></div> <script type="text/javascript"> var div = document.getElementById("textDiv"); div.textContent = "my text"; var text = div.textContent; </script>
In this example, the value of
textis "my text". However, the value resulting from getting or setting thetextContentproperty on a parent node might include text content from the node's children. The following example shows that thetextContentthat is set on a child node is included in the value oftextContentof the parent node:<div id="textDiv"> <div id="nested"></div> </div> <script type="text/javascript"> var div = document.getElementById("textDiv"); var nestedDiv = document.getElementById("nested"); nestedDiv.textContent = "nested"; var text = "[" + div.textContent + "]"; </script>
In this example, the value of
textis "[nested]". -
You can also create an element and write to its innerHTML or innerText properties. Setting these properties affects only the text in the element itself, not in its children. However, these properties also have some disadvantages:
-
The innerText property doesn't work in all browsers, so you might want to avoid it for reasons of compatibility.
-
The innerText property is affected by CSS styles, and doesn't appear if the element is hidden.
-
The innerHTML property gets and sets both nested nodes and text. If it isn't secured, it could be used for script-injection attacks. In addition, setting it to text without HTML tags removes all previously set nodes.
-
-
You can use the
document.writemethod without having to create an element. However, using this method causes the entire web page to be cleared, which might not be what you want.The following example shows one of the disadvantages of using
document.write. The script is intended to display the time every 5 seconds, but it shows the time just twice. By the timedocument.writeis called the second time, the page has finished loading, anddocument.writethen clears the entire page (it callsdocument.open). At this point, theShowTimefunction no longer exists.<!DOCTYPE html> <html> <head> <script type="text/javascript"> function ShowTime() { var dt = new Date(); document.write(dt.toTimeString()); // var elem = document.getElementById("divElem"); // elem.textContent = dt.toTimeString(); window.setTimeout("ShowTime();", 5000); } </script> </head> <body> <script type="text/javascript"> ShowTime(); </script> <div id="myDiv"></div> </body> </html>
To fix the preceding code, remove the line of code that contains
document.writeand uncomment the two commented lines of code that follow. -
You can also use an
alert``prompt, orconfirmfunction, which displays a message in a pop-up window. In most cases it's not a good idea to use a pop-up window in a web browser. Most modern browsers have settings that automatically block pop-up windows, so your message might not be seen. Moreover, it is possible to enter an infinite loop when you use pop-up windows, which makes it impossible for the user to close the web page in the usual way.