473,792 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

advantage of document.write

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?

Jun 9 '06 #1
9 2206
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.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 9 '06 #2
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.

Jun 9 '06 #3
sonnystarks wrote:
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.create TextNode(getDat e());
document.getEle mentById('h1'). appendChild(tn) ;
</script>

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.
--
Rob
Jun 9 '06 #4
RobG wrote:
... 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.

--
Rob
Jun 9 '06 #5
RobG wrote:
RobG wrote:
... 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.


With the exception of the W3C HTML DOM levels 1 and 2. ;-)

Richard.

Jun 9 '06 #6


sonnystarks wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 9 '06 #7
> sonnystarks wrote:

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.

Jun 9 '06 #8
ra****@gmail.co m said the following on 6/9/2006 11:16 AM:
sonnystarks wrote:

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


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.clipboa rdData) {
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.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 9 '06 #9
In article <11************ **********@u72g 2000cwu.googleg roups.com>, "sonnystark s" <si***********@ comcast.net> wrote:
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.


Sonny,

Much if not most of what you do in a class to learn is not relevant to the
real world in its early form, but teaches you lessons you will need to
understand so as to be able to do something more useful later. Using
Javascript to write a line of fixed Header text would be, to anyone in this
group, ludicrous, and mostly just a waste of time. However, for you to learn
how to do so, then gives you the tools you need to do something more useful
later, such as your Header saying a simple "Good Morning", "Afternoon" or
Evening" pending the time of day the page is viewed, as controlled by
Javascript.

Don't assume all in education makes sense until you're further along. In
programming it often doesn't!

Take a little time to be sure you understand th ebasics well. You know how you
eat an elephant? One byte at a time. Preogrammin is very much the same!

Larry L [in Honolulu]

Jun 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2874
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>"); document.write("<OPTION VALUE='0.345066110642241'>Argentina Peso </OPTION>"); document.write("<OPTION VALUE='0.790200069503053'>Australia Dollar
3
2524
by: Ike | last post by:
Can anyone discern why the following code writes the document.write() lines literally? That is, a line like document.write('<CENTER>') should write <CENTER> but instead writes the entire document.write('<CENTER>'). I must be really doing something stupid. Can anyone here illuminate me? Thanks, Ike <HTML><HEAD> <TITLE>My Applet</TITLE> </HEAD><BODY>
2
2386
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of copying/pasting everytime I change it, I figure this will be better, as I only change it once. The problem is, document.write doesn't handle multiple lines very well, so I was wondering what is the best way to do this? Maybe there is even a better...
13
9651
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
14
4130
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as it's being written into the page. Is there a way that I can view what is being placed into the page, instead of, or in addition to the javascript code?
4
2953
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400, like DOS, uses memory-mapped display, two bytes per character (one char byte and one attribute byte). We can get the screen offset allright, and I've written a javascript which does the math to convert the offset into row/col (i.e. left, top)...
2
5783
by: bissatch | last post by:
Hi, I am trying to use JavaScript to write a table column on a web page. The code is as follows: <html> <head> <script> function displaycount() {
11
460
by: Tony | last post by:
Is it me, or is document.write just about the most abused js function? Maybe, like goto, js would be better without it? Is there any good reason to use it? Because I'm having a hard time seeing any... Maybe my imagination just isn't good enough :)
0
9670
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.