Export (0) Print
Expand All

Displaying Information in the Browser 

Although browsers support most JScript features, the new features that target the .NET Framework, class-based objects, data types, enumerations, conditional compilation directives, and the const statement, are supported only on the server-side. Consequently, you should use these features exclusively in server-side scripts. For more information, see JScript Version Information.

Whenever a script is intended to run in a browser (client-side), experienced developers include code that detects the version of the script engine. After the script detects the engine version, it can redirect the browser to a page with script that is compatible with the browser's script engine. For more information, see Detecting Browser Capabilities.

JScript displays information in a browser through the write and writeln methods of the browser's document object. It can also display information in forms within a browser and in alert, prompt, and confirm message boxes. For more information, see Using Message Boxes.

Using document.write and document.writeln

The most common way to display information is the write method of the document object. It takes one argument, a string, which it displays in the browser. The string can be either plain text or HTML.

Since strings can be enclosed in either single or double quotation marks, you can quote something that contains quote marks or apostrophes.

document.write("Pi is approximately equal to " + Math.PI);
document.write();
NoteNote

The following simple function enables you to avoid typing document.write every time you want text to appear in the browser window. This function does not inform you if something that you attempt to write is undefined, but it does let you issue the command w();, which displays a blank line.

function w(m) { // Write function.
   m = String(m); //  Make sure that the m variable is a string.
   if ("undefined" != m) { // Test for empty write or other undefined item.
      document.write(m);
   }
   document.write("<br>");
}

w('<IMG SRC="horse.gif">');
w();
w("This is an engraving of a horse.");
w();

The writeln method, which is almost identical to the write method, appends a newline character to the provided string. In HTML this ordinarily results only in a space after an item; within <PRE> and <XMP> tags, the newline character is interpreted literally and the browser displays it.

The write method opens and clears the document if the document is not in the process of being opened and parsed when the write method is called. This poses potentially unexpected results. The following example, which shows a script that is intended to display the time once a minute, fails to do so after the first time because it clears itself in the process.

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JScript">
function singOut()  {
   var theMoment = new Date();
   var theHour = theMoment.getHours();
   var theMinute = theMoment.getMinutes();
   var theDisplacement = (theMoment.getTimezoneOffset() / 60);
   theHour -= theDisplacement;
   if (theHour > 23)  {
      theHour -= 24
   }
   // The following line clears the script the second time it is run.
   document.write(theHour + " hours, " + theMinute + " minutes, Coordinated Universal Time.");
   window.setTimeout("singOut();", 60000);
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT>
singOut();
</SCRIPT>
</BODY>
</HTML>

If you use the alert method of the window object instead of document.write, the script works.

   // This line produced the intended result.
   window.alert(theHour + " hours, " + theMinute + " minutes, Coordinated Universal Time.");

The element.innerText or element.innerHTML is preferred in Internet Explorer Version 5 and greater.

Clearing the Current Document

The clear method of the document object empties the current document. This method also clears your script (along with the rest of the document), so be very careful how and when you use it.

document.clear();

See Also

Community Additions

ADD
Show:
© 2014 Microsoft