PDA

View Full Version : document.write doesn't work but should!



grasshopperwisdo
03-14-2008, 05:39 AM
Hello world.

Yes, the simple statements below are in an html file called helloworld.html
and the browser does not seem to matter, I tried with IE
SeaMonkey 1.1.7, Safari 2.0.4, IE 5.2

<html>
<head>
<script type="text/javascript">
function w1() {
document.write('hello world');
}
</script></head>
<body>
<script type="text/javascript">
w1();
document.write('goodbye moon');
</script>

</body>
</html>

do not work, for the life of me I don't see what's up?!

The tippity top of the document has
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Suggestions??
A BIZILLION TRILLION INFANTABALE thanks....

:confused::eek:

rangana
03-14-2008, 05:47 AM
How do you want this to work?..It shows up to me in IE7 and FF.

But first, you have to avoid joining your js (presentation) with your markups.
Perhaps, you might find this useful:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
window.onload = function (){
document.write('hello world');
document.write('goodbye moon');
}
</script></head>
<body>
</body>
</html>


See if it helps ;)

grasshopperwisdo
03-14-2008, 08:25 AM
I was trying it both places.

Are you telling me js does not work in the body of an html file?

Seems to me I need to get the layers of interpretation sorted out.

There's, upon loading... I guess that is in a function?
What I'd like to do is see if I am getting the cookie passed to me, and
the only way to verify that is to print it to the screen. I was testing
the write.document function.

The code I wrote came from a website that was a tutorial.

I see js in the html body all over the place, so I'd really like to get the deeper understanding here....

Many thanks for your reply...

Cindy:D:confused:

rangana
03-14-2008, 08:30 AM
Are you telling me js does not work in the body of an html file?

No, js still works in the body section. It's just made the markups messy.

What do you want to achieve out from it?..and using document.write is not always a good option because it replaces the contents of the current document, and produces unpredictable results ;)

If you'll be more specific on what you want to achieve, then we'll try to explain better :)

grasshopperwisdo
03-14-2008, 09:04 AM
I am passing the cookie information into the file and want to print it out to make
sure we got it. but how do i check that without printing it somehow, as a debug
statement?

Is there a straight forward way of explaining how all the different interpretations
take place? Like, why didn't that function work? Or, why would did you put the
document.write statements in the function? "messy" for me, doesn't give me an
understanding of "why" it didn't work...

perhaps explaining why is too complicated.

rangana
03-14-2008, 09:12 AM
Having it this way:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function w1() {
document.write('hello world');
}
</script></head>
<body>
<script type="text/javascript">
w1();
document.write('goodbye moon');
</script>

</body>
</html>



..and the code I suggested above works the same.
I don't get it on what you mean about "it don't work"?
That code outputs hello worldgoodbye moon in the screen. What's wrong with it ;)

grasshopperwisdo
03-14-2008, 09:13 AM
The W3C webpage on javascript says this code should print hello world. On
my browser, it does not work. What's UP? Please don't just tell me its messy.
I would like to understand... what's wrong?

<html>
<body>

<script type="text/javascript">
document.write("Hello World!");
</script>

</body>
</html>

codeexploiter
03-14-2008, 09:14 AM
If you use document.write as a part of the 'onload' event of the window object then it will erase any existing content in that page and insert only the one you've inserted through the document.write method.

But if you use document.write before the completion of the page rendering then the information you insert using document.write will be inserted in your page along with other content the page already has.

Eg 1:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">

</style>
<script type="text/javascript">
window.onload = function(){
document.write('Hello World ');
}
</script>
</head>
<body>
<div>This is a test </div>
</body>
</html>


If you open the page you can only view 'Hello World' in your page as we've used document.write method to insert some information in to a page which has rendered completely.

Eg 2:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> </title>
<style type="text/css">

</style>
<script type="text/javascript">

document.write('Hello World ');

</script>
</head>
<body>
<div>This is a test </div>
</body>
</html>


If you open the above page in browser it will show 'Hello World' then a line break and 'This is a test', linebreak comes because we've used <div> element to wrap 'This is a test'.

You can insert a <script> element within <head> or <body>. But normally developers use the <head> section.

It is always better if you not mingle your markup with your JavaScript section as it will make reading of the web page more difficult from a developer's point of view so what a developer do is :

- If you have large volume of JavaScript in a page then they'll save it as a different .js file and include that file in to the web page like the following manner

<script type="text/javascript" src="myfuns.js"></script>

"myfuns.js" is a file which contains only JavaScript code.

If you want to improve the look and feel of the information in the web page again you can append the style information within the markup or can have a CSS file and include that like the following manner:

<link rel="stylesheet" type="text/css" href="mystyles.css" />

After including the CSS file in your web page you can apply the classes, IDs, etc in your page without messing your (X)HTML markup.

grasshopperwisdo
03-14-2008, 09:14 AM
It doesn't print ANYTHING on my browser, that's what I mean,
it doesn't work...

codeexploiter
03-14-2008, 09:16 AM
The W3C webpage on javascript says this code should print hello world. On
my browser, it does not work. What's UP? Please don't just tell me its messy.
I would like to understand... what's wrong?

<html>
<body>

<script type="text/javascript">
document.write("Hello World!");
</script>

</body>
</html>

Have you disabled JavaScript in your browser? If so enable it. Your code works fine for me.

rangana
03-14-2008, 09:33 AM
There it goes. The real problem is that js is disabled.
To enable, you might find this site useful:

http://support.microsoft.com/gp/howtoscript
http://tip.it/runescape/?page=enable_js.htm

See if it helps ;)