Back

Here's an example of a simple function containing an argument which will be defined as a text string, then uses document.write() to write the value of the argument to this document. Since the write statement is within a function, we need to open and close the stream.

function writing(myString) {   //declare argument to be used

alert(myString);

document.open();   //open the stream

document.write(myString);   // here is where argument is used

document.close();   //close the stream

}


The argument is named "myString" and is defined when the function is called: from within the form button's onClick event handler.

<form>
<input type="button" onClick="writing( ' Hello World! ' )" value="Hello World!">
</form>


The function's first statement is an alert that shows the value of the "myString" variable. Since the alert statement comes before the write statement, we'll see it first.

View the Source