Broad Network


ECMAScript Outputs

ECMAScript Basics - Part 2

Forward: In this part of the series, I explain how to output or display ECMAScript Information.

By: Chrysanthus Date Published: 25 Jul 2012

Introduction

This is part 2 of my series, ECMAScript Basics. In this part of the series, I explain how to output or display ECMAScript Information.

If you think anything is missing for this article, just contact me at forchatrans@yahoo.com

The Alert Box
ECMAScript has a box, which you can use to display ECMAScript information. This box usually appears like a small rectangle and it is usually gray in color. The syntax to display information is:

    alert(string);

You have the word “alert”. After the word you have parentheses. Inside the parentheses you have text in single or double quotes. It is this text that is displayed in the alert box. The whole line above that ends with a semi-colon, is an example of what is called an ECMAScript statement. The word “alert” with its parentheses is an example of what is called a function.

Assume that you want to display the following string:

    “I love ECMAScript.”

The statement you type, would be:

    alert("I love ECMAScript.");

It could also be:

    alert('I love ECMAScript.');

The first alert statement uses double quotes. The second one uses single quotes. At this point, I should warn you about quotations. The double or single quotation marks offered by your word processor (e.g. Microsoft Word), are not the ones you should use in your code. The quotation marks you use should be the ones offered by your text editor. The quotation marks offered by your text editor are not the same in shape as the ones offered by your word processor.

Example of Alert Box
The following code will display an alert box with the content of the string, "I love ECMAScript."

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
    alert('I love ECMAScript.');
</script>
</body>
</html>

Try the above code. You should see the alert box, with the sentence.

Inserting into the Page
You can insert information into the current page. The information will appear below what is already on the page. The syntax for the statement is:

    document.write(string);

You begin with the word, “document”; this is followed by a dot, then the word, “write”, and then, parentheses. Inside the parentheses you have a string. The word, document, here, represents what is called an Object, in ECMAScript. The word, “write” with parentheses is an example of what is called a function.

The following code will insert the sentence, “I love ECMAScript.” into the current page:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
    Current Page.
<script type="text/ECMAScript">
    document.write('I love ECMAScript.');
</script>
&nbsp;Current Page.
</body>
</html>

Try the code. In the code, the string is in single quotes. You can still use double quotes. At the browser, the string is inserted at the position where the ECMAScript is.

Replacing HTML Content
You can use ECMAScript to replace the content of an HTML element. You can use it to replace the content of a paragraph element, for example. You need to understand other ECMAScript principles first, before you can understand how to do this. So we shall look at this later.

ECMAScript Statement
A statement in ECMAScript is a short piece of code that usually ends with a semicolon.

ECMAScript Version
The latest version of ECMAScript is version 5. In this tutorial series, all what I give you is based on version 5.

Let us stop here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message