Question

In: Computer Science

what is dynamic document in javascript. why this is important. how can you position elements in...

what is dynamic document in javascript. why this is important. how can you position elements in a dynamic document, how can you create dynamic content, what actions take place when the user clicks a mouse.

Solutions

Expert Solution

ally Generated Documents

One of the most important features of the Document object (and perhaps of client-side JavaScript in general) is the write( ) method, which allows you to dynamically generate web-page content from your JavaScript programs. This method can be used in two ways. The first and simplest way to use it is within a script, to output dynamically generated HTML into the document that is currently being parsed. This was discussed in Chapter 12. Consider the following code, which useswrite( ) to add the current date and the document's last-modified date to an otherwise static HTML document:

<script>
var today = new Date( );
document.write("<p>Document accessed on: " + today.toString( ));
document.write("<br>Document modified on: " + document.lastModified);
</script> 

Using the write( ) method in this way is an extremely common JavaScript programming technique, and you'll see it in many scripts.

Be aware, however, that you can use the write( ) method to output HTML to the current document only while that document is being parsed. That is, you can call document.write( ) from within <script> tags only because these scripts are executed as part of the document parsing process. In particular, if you call document.write( ) from within an event handler and that handler is invoked once the document has already been parsed, you will end up overwriting the entire document (including its event handlers), instead of appending text to it. The reason for this will become clear as we examine the second way to use the write( ) method.

In addition to adding dynamic content to the current document as it is being parsed, write( ) can be used in conjunction with the open( ) and close( ) Document methods to create entirely new documents within a window or frame. Although you cannot usefully write to the current document from an event handler, there is no reason why you can't write to a document in another window or frame; doing so can be a useful technique with multiwindow or multiframe web sites. For example, JavaScript code in one frame of a multiframe site might display a message in another frame with code like this:

<script>
// Start a new document, erasing any content that was already in frames[0]
parent.frames[0].document.open( );
// Add some content to the document
parent.frames[0].document.write("<hr>Hello from your sibling frame!<hr>");
// And close the document when we're done
parent.frames[0].document.close( );
</script>

To create a new document, we first call the open( ) method of the Document object, then call write( ) any number of times to output the contents of the document, and finally call the close( ) method of the Document object to indicate that we have finished. This last step is important; if you forget to close the document, the browser does not stop the document loading animation it displays. Also, the browser may buffer the HTML you have written; it is not required to display the buffered output until you explicitly end the document by calling close( ).

In contrast to the close( ) call, which is required, the open( ) call is optional. If you call the write( ) method on a document that has already been closed, JavaScript implicitly opens a new HTML document, as if you had called the open( ) method. This explains what happens when you call document.write( ) from an event handler within the same document -- JavaScript opens a new document. In the process, however, the current document (and its contents, including scripts and event handlers) is discarded. This is never what you want to do, and it can even cause some early browsers (such as Netscape 2) to crash. As a general rule of thumb, a document should never call write( ) on itself from within an event handler.

A couple of final notes about the write( ) method. First, many people do not realize that the write( ) method can take more than one argument. When you pass multiple arguments, they are output one after another, just as if they had been concatenated. So instead of writing:

document.write("Hello, "  + username + " Welcome to my home page!"); 

you might equivalently write:

var greeting = "Hello, ";
var welcome = " Welcome to my home page!";
document.write(greeting, username, welcome);

The second point to note about the write( ) method is that the Document object also supports a writeln( ) method, which is identical to the write( ) method in every way except that it appends a newline after outputting its arguments. Since HTML ignores line breaks, this newline character usually doesn't make a difference, but as we'll see in a bit, the writeln( ) method can be convenient when you're working with non-HTML documents.


Related Solutions

1. What does the effectiveness of a document depend on, and what are some important elements to consider when designing a document?
Topic: “Designing Print and Online Documents,”Reflect and respond (answer one or more of the following questions):1. What does the effectiveness of a document depend on, and what are some important elements to consider when designing a document?What are similarities between designing a print document and online document?What images and visuals are you including on your site and how do they aid in connecting to your audience and furthering your purpose?Word count: 400 words
What are the data elements used to describe an object's position and motion? How can satellite...
What are the data elements used to describe an object's position and motion? How can satellite orbits be classified (i.e. put into categories)?
What is the difference between an active document and a dynamic document? Give example of those...
What is the difference between an active document and a dynamic document? Give example of those document! In electronic mail, what is MIME? Why do we need POP3 or IMAP4 for electronic mail? Explain
What are project assumptions and provide an example as to why it is important to document...
What are project assumptions and provide an example as to why it is important to document them.(answer in a paragraph with at least 75 words )
What are the basic elements of the monetarists’ position? Do they seem justified? Why?
What are the basic elements of the monetarists’ position? Do they seem justified? Why?
What are the four elements of the marketing mixdescribed in the document?What is the...
What are the four elements of the marketing mix described in the document?What is the value that the organization delivers to its customers?
1.JavaScript is a dynamic and loosely typed language. What is the significance of these language descriptors?...
1.JavaScript is a dynamic and loosely typed language. What is the significance of these language descriptors? 2.JavaScript variables are hoisted at run time. What does this mean and how does it impact function design? 3.Define a variable in JavaScript that is assigned an anonymous function. This function should take two values as input and give back the sum 4. Most of the JavaScript use in Web Development is through the DOM. What does DOM stand for? 5.Two of the primary...
Discuss the elements of a Tenant’s Request for Proposal and why this document is of value...
Discuss the elements of a Tenant’s Request for Proposal and why this document is of value to a prospective tenant.
Define dynamic forces within healthcare and outline why the dynamic forces in the industry are important...
Define dynamic forces within healthcare and outline why the dynamic forces in the industry are important to professionals. Identify challenges requiring quality leadership intervention strategies. Lastly, make connection explaining how these strategies can help you be more successful within a leadership position.
Explain as much as you can. What is motivation, and why is it important in the...
Explain as much as you can. What is motivation, and why is it important in the study of consumer behavior? Can motives be measured, and if so, how? [10 pts]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT