Home arrow JavaScript arrow Page 3 - Roots of Crys`s ACP Document Phase Approach
JAVASCRIPT

Roots of Crys`s ACP Document Phase Approach


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.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
July 10, 2009
TABLE OF CONTENTS:
  1. · Roots of Crys`s ACP Document Phase Approach
  2. · How does one open a new document from the current document?
  3. · How do I send HTML contents to a newly opened document?
  4. · Can you access the HTML content of the newly loaded document?

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials