Displaying Information in the Browser

JScript displays information in a browser by using the write method 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.

Note

For additional information about how to write scripts that run on a client computer in a Web browser, see JScript (Windows Script Technologies).

Using document.write

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.

Strings can be enclosed in either single or double quotation marks. This enables you to quote something that contains quotation marks or apostrophes.

The following example illustrates the use of the write method.

document.write("Pi is approximately equal to " + Math.PI);
// New line.
document.write("<br />");
document.write("This is an engraving of a horse.");
// New line.
document.write("<br />");
document.write('<IMG SRC="horse.gif">');

When you call the write method, the document is opened and cleared if it is not being opened and parsed when the write method is called. This causes potentially unexpected results. The following example shows a script that is intended to display the time every 5 seconds, but it cannot do so after the first time because it clears itself in the process.

<html>

<head>
<script language="JScript" type="text/jscript">
    function ShowTime()
    {
        var dt = new Date();
        document.write(dt.toTimeString());
        window.setTimeout("ShowTime();", 5000);
    }
</script>
</head>

<body>
<script language="JScript" type="text/jscript">
    ShowTime();
</script>
</body>

</html>

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

        window.alert(dt.toTimeString());
        window.setTimeout("ShowTime();", 5000);
    }

See Also

Concepts

Using Message Boxes

Detecting Browser Capabilities

JScript Version Information

Other Resources

Displaying Information with JScript