473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

JavaScript in HTML: unexpected outputs

[My initial posting didn't seem to make it, so here's retry]

I'm learning/experimenting with some simple JS/html markup, running
an apache daemon and mozilla firefox browser in RH 9.

Let's say I run the following markup with one or more of lines 6-10
commented out:

1 <html>
2 <head>
3 <script type="text/javascript">
4 function showinfo()
5 {
6 width = window.screen.availWidth;
7 document.write("<br>screen width=\n");
8 document.write(width);
9 document.write("<br>Date: ");
10 document.write(Date());
11 }
12 </script>
13 </head>
14 <body onload="showinfo()">
15 <p>

My question: is there a simple explanation to account for the 'unexpected'
behaviours below (Runs 1, 3, 5)?

Run 1: nothing in 6-10 commented out; output (expected more):

screen width=

Run 2: line 7 commented out; output (as expected):

1024
Date: Wed Apr 14 2004 20:55:58 GMT-0400 (EDT)

Run 3: lines 7, 9, 10 commented out; output (as expected, but unfinished loading):

1024
[ongoing hourglass indicating loading in progress]

Run 4: lines 7, 10 commented out; output (as expected):

1024

Run 5: lines 7-9 commented out; output (as expected, but unfished loading):

Date: Wed Apr 14 2004 22:46:49 GMT-0400 (EDT)

I'm sure I'm missing something obvious. Helpful comments would be much appreciated.

--Tony
Jul 23 '05 #1
2 1565
Tony Gahlinger wrote:
<snip>
4 function showinfo()
5 {
6 width = window.screen.availWidth;
7 document.write("<br>screen width=\n");
8 document.write(width);
9 document.write("<br>Date: ");
10 document.write(Date());
11 }
12 </script>
13 </head>
14 <body onload="showinfo()">

<snip>

After a document has closed (which will have happened before the onload
event) any call to - document.write - will clear and replace the current
document with whatever is written. But the function - showinfo - is
associated with the current document and is cleared, while it is
executing. Unsurprisingly the consequences of removing the environment
of a script while it is executing tend to be inconsistent and
undesirable.

If you want to call document.write after the current page has closed I
would recommend outputting all of the new contents with just one call
to - document.write - rather than several.

Also, if a - document.open - call is made, or a - document.write - call
is made on a document that is closed (which internally calls the open
method when the document is not currently open), the document is not
re-closed until the - document.close - method is called. Hence the
impression that the page has not finished loading.

Richard.
Jul 23 '05 #2
Richard Cornford wrote:
Tony Gahlinger wrote:
<snip>
4 function showinfo()
5 {
6 width = window.screen.availWidth;
7 document.write("<br>screen width=\n");
8 document.write(width);
9 document.write("<br>Date: ");
10 document.write(Date());
11 }
12 </script>
13 </head>
14 <body onload="showinfo()">


<snip>

After a document has closed (which will have happened before the onload
event) any call to - document.write - will clear and replace the current
document with whatever is written. But the function - showinfo - is
associated with the current document and is cleared, while it is
executing. Unsurprisingly the consequences of removing the environment
of a script while it is executing tend to be inconsistent and
undesirable.

If you want to call document.write after the current page has closed I
would recommend outputting all of the new contents with just one call
to - document.write - rather than several.

Also, if a - document.open - call is made, or a - document.write - call
is made on a document that is closed (which internally calls the open
method when the document is not currently open), the document is not
re-closed until the - document.close - method is called. Hence the
impression that the page has not finished loading.

Richard.


Ah ... that makes sense all right; especially the reason for the unfinished
loading (which one sees so often). Thanks!

--Tony
Jul 23 '05 #3

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

Similar topics

9
by: Charlene Russ | last post by:
Learn on-line at your own in a user-centered format with plenty of interaction and personal attention. This is a basic level coursed designed to introduce the novice to intermediate computer...
3
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Keep in mind that i am not a javascript programmer but php. Here is...
2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
4
by: Ian Cox | last post by:
I have a web form that contains a Datagrid. This grid has a number of columns, one of which contains a text box and validator for that text box. Everything works fine, when I press the "Save"...
4
by: Ken Nipper | last post by:
Does anyone know how to access the menu items from javascript. I can use the onclick to perform an action but I would like to be enable/disable items based on page status. Thanks Ken
7
by: psybert | last post by:
Hello everyone, Long time lurker, first time poster. I'm a beginner coder, and I've taught myself everything with the help and expertise of users and websites like this one. I normally figure out...
1
by: psybert | last post by:
(cross posted from JavaScript forum) Hello everyone, Long time lurker, first time poster. I'm a beginner coder, and I've taught myself everything with the help and expertise of users and websites...
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
3
by: jackson.rayne | last post by:
Hello, Another newbie question here. Let me explain my situation first. I have bought a 3rd party tool that runs a PHP script and gives me some HTML code which I can directly use in my...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.