In this second part of a twelve-part series on Active Client Pages (ACP), I give you the roots that give rise to my approach. One way to learn is by asking questions. We shall use that learning method here. Some of the questions are: can you reference a previously opened page? How can you get and set contents in a page that has just been loaded? When DOM was invented, Active Client Pages technology was not envisioned, so we have to ask these questions.
Roots of Crys`s ACP Document Phase Approach - How do I send HTML contents to a newly opened document? (Page 3 of 4 )
The purposes of the open() and close() methods are as follows:
open():Opens a stream to collect the output from any document.write() or document.writeln() methods.
close(): Closes an output stream opened with the document.open() method, and displays the collected data.
For our aim in this series, you open a document using the above two methods. To answer the question, write in between these two methods. In this series we shall use the write() method to do this.
The argument of this write() method is a string. If you have entities in this string, escape them. When you click the button in the following code, a new document should be opened with the following text in it. Well the coding is not very good, but we shall improve it.
“This is the second page.”
<html>
<head>
<script type="text/JavaScript">
function openWrite()
{
document.open();
document.write('This is the second page.');
document.close();
}
</script>
</head>
<body>
<button type="button" onclick="openWrite()">Open New Document from Current Document</button>
</body>
</html>
This code works, but there is an ambiguity that may cause problems as we go ahead in the series. When you click the button, the openWrite() function is called. The first statement in the function opens the output stream; the next statement writes text into the opened stream. The last statement closes the stream, thereby concluding the opening process of the document as we said.
Now all the statements in the function begin with “document.” In this case, "document” refers to the current document. However, we have used it to write to a newly-opened document. So it is not clear here whether “document” is referring to the document that loads the new document or is referring to the newly-loaded document. In this case, it is actually referring to the newly-loaded document. With JavaScript, there are situations where “document” refers to the current document and situations where "document" refers to the loaded document. Fortunately there is a way for us to indicate the difference.
The “document.open()” expression returns a reference to the newly-opened document. This reference can be used to address the newly-opened document while the script in the page that opened the new page is still running. The following code repeats the above, but shows how to use the reference.
<html>
<head>
<script type="text/JavaScript">
function openWrite()
{
var pageTwo = document.open();
pageTwo.write('This is the second page.');
pageTwo.close();
}
</script>
</head>
<body>
<button type="button" onclick="openWrite()">Open New Document from Current Document</button>
</body>
</html>
When the new document is opened by the first statement, the reference is pageTwo. This reference is used for the rest of the statements. With this reference, it is clear to which document we are referring; we are referring to the newly-opened document with this reference.