advantage of document.write

S

sonnystarks

I am taking a course in writing javascript and it (and all the books I
have been reading) tell me that if I will use the document.write
syntax, I will be able to "place text on the page."

None of these sources tell me why I would do that instead of using
ordinary HTML which is less typing.

(I think I understand that it is somehow necessary if I am going to
include text in a javascript function such as a calendar/clock, but no
source comes out directly and says that.)

Thoughts?
 
R

Randy Webb

sonnystarks said the following on 6/8/2006 9:25 PM:
I am taking a course in writing javascript and it (and all the books I
have been reading) tell me that if I will use the document.write
syntax, I will be able to "place text on the page."

Only before the page has finished loading. If the page has finished
loading, then document.write will clear the current page and replace it
with whatever you document.write
None of these sources tell me why I would do that instead of using
ordinary HTML which is less typing.

Dynamic content based on parameters.
(I think I understand that it is somehow necessary if I am going to
include text in a javascript function such as a calendar/clock, but no
source comes out directly and says that.)

If the document.write call is in a function and that function is called
after the page has loaded, it will clear the current page and replace
it. If the function is called prior to the page loading then it will add
content to the page.
 
S

sonnystarks

I'm sorry but your answer is a clear as mud... not your fault, I'm sure
but mine. I am still not getting it.

Let's use an example please. The course I am taking says to put my <h1>
tags within the javascript syntax. Now, if Johnny Doe goes to my
website in this example, what difference will he see when he looks at
<h1>This is a headline</h1> in ordinary HTML and if it is in the
Javascript syntax? I don't grasp the "Only before the page has finished
loading. If the page has finished loading, then document.write will
clear the current page and replace it
with whatever you document.write" concept.

I have looked at examples of javascript code and understand what it
does in the final appearance, " as in a clock or calendar, > Dynamic
content based on parameters." but don't understand how it is an
advantage in ordinary <a href> or <h?> tags?

Is it an example of "speed" while the page is loading? Is this what you
are trying to tell me? Is it something to do with the webpage refresh
rate?

Thanx a million for the patience. It just seems the book and course are
assuming I know something I don't.
 
R

RobG

sonnystarks said:
I'm sorry but your answer is a clear as mud... not your fault, I'm sure
but mine. I am still not getting it.

Let's use an example please. The course I am taking says to put my <h1>
tags within the javascript syntax. Now, if Johnny Doe goes to my
website in this example, what difference will he see when he looks at
<h1>This is a headline</h1> in ordinary HTML and if it is in the
Javascript syntax?

If he doesn't have javascript enabled or available, he won't see the
heading :)

I don't grasp the "Only before the page has finished
loading. If the page has finished loading, then document.write will
clear the current page and replace it
with whatever you document.write" concept.

It means if you wait until the page has finished loading, then
document.write your heading, it will be the only thing on the page.

I have looked at examples of javascript code and understand what it
does in the final appearance, " as in a clock or calendar, > Dynamic
content based on parameters." but don't understand how it is an
advantage in ordinary <a href> or <h?> tags?

In that particular example, it means you can easily write the date to
the page based on the users system clock rather than your server clock -
document.write is not the only way to do that.

As a quick 'n dirty example:


<title>foo</title>
<script type="text/javascript">
function getDate(){
var now = new Date();
return now.getFullYear() + '.'
+ (now.getMonth()+1) + '.'
+ now.getDate();
}
</script>

<!-- document.write -->

<h1><script type="text/javascript">
document.write(getDate());
</script></h1>

<!-- DOM equivalent -->

<h1 id="h1"></h1><script type="text/javascript">
var tn = document.createTextNode(getDate());
document.getElementById('h1').appendChild(tn);
Is it an example of "speed" while the page is loading? Is this what you
are trying to tell me? Is it something to do with the webpage refresh
rate?

None of the above. document.write is a relic of very early JavaScript,
it is not part of the W3C DOM. It belongs to "DOM 0", which is the
collection of functionality that was common between IE and Netscape
about the time the W3C published their DOM 1. Other browsers also
implement DOM 0 stuff, document.write is likely supported by every
single browser that also supports scripting.
 
R

RobG

RobG said:
... It belongs to "DOM 0", which is the
collection of functionality that was common between IE and Netscape
about the time the W3C published their DOM 1

but was not included in the W3C DOM.
 
M

Martin Honnen

sonnystarks said:
Let's use an example please. The course I am taking says to put my <h1>
tags within the javascript syntax. Now, if Johnny Doe goes to my
website in this example, what difference will he see when he looks at
<h1>This is a headline</h1> in ordinary HTML and if it is in the
Javascript syntax?

Of course using a programming language to generate your HTML makes only
sense if you have some dynamic content (e.g. date/time) you generate
with the programming language or you have data you want to write out.
 
R

radykl

sonnystarks said:
Of course using a programming language to generate your HTML makes only
sense if you have some dynamic content (e.g. date/time) you generate
with the programming language or you have data you want to write out.

But in general, if you want to generate a page with dynamic content, it
would be better to use a server/side technology like jsp, aspx, php,
etc... You would use Javascript more to react to users actions in the
browser, not to generate the page in the first place, therefore for
practical purposes and in my experience document.write is nothing you
typically use.

/Andres.
 
R

Randy Webb

(e-mail address removed) said the following on 6/9/2006 11:16 AM:
But in general, if you want to generate a page with dynamic content, it
would be better to use a server/side technology like jsp, aspx, php,
etc...

Depending on where that dynamic content comes from.
You would use Javascript more to react to users actions in the
browser, not to generate the page in the first place, therefore for
practical purposes and in my experience document.write is nothing you
typically use.

Really?

Question 1: Sow would you create a button that can only be used if
Javascript is enabled?

Second question: How would you add a button that copies text to the
clipboard?

Third question: How would you add the current date - according to my
system clock - with server side code?

Server side code please.

Client-side code:

Answer 1: document.write ('button code here');
Answer 2:
if (window.clipboardData) {
document.write('<input type="button" onclick="copyIt()" value="Copy It">');
}

Where copyIt() is a function that copies text to the clipboard.
document.write has it's place, if you know what that place is.

Answer 3: document.write(new Date());
At it's simplest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top