Re: Functions assigned to variables test



On Mar 20, 11:12 pm, "tshad" <t...@xxxxxxxxxxxxxx> wrote:
Peter Michaux wrote:
On Mar 19, 11:36 pm, "tshad" <t...@xxxxxxxxxxxxxx> wrote:

[snip]

So how does the order work.

The Javascript global variables and functions get called as they are
rendered.

The code in the <script> elements is executed when that HTML element
is parsed. (Unless there is a
"defered"attribute but that doesn't apply to your examples.)

That is kind of what I figured.

So the scripts are handled as soon as the </script> is reached and no more
rendering is done until it is finished with setting up any global variables
and functions that are called directly from that script and not in a
function.

Just so I understand here if I have the following page:
************************************************

HTML 4.01 Transitional or Strict doctype goes here and you can
validate your page.

http://validator.w3.org/

<html>
<head>
<title></title>
<script type="text/javascript">
var something = "a test";
var isomethingelse = 10;

function printOnWindowLoad()
{
alert("This is the startup routine from WindowOnLoad");
}

function printOnBodyLoad()
{
alert("This is the Body onload routine");
}

function sf()
{
alert("before focus");
document.theForm.second.focus();

It is safer to write the following in case of namespace collisions

document.forms.theForm.elements.second.focus();

}
sf();
</script>

</head>
<body onload="printOnBodyLoad();">

<script type="text/javascript">
alert(window.onload);
</script>

<form name="theForm">
<input maxlength=2048 name=first size=55 title="Search"

better to use double quotation marks around attributes

name="first"

value=""><br><br>
<input maxlength=2048 name=second size=55 title="Search" value="">

<script type="text/javascript">
var temp = "This is temp";
sf();
</script>

</form>
</body>
</html>
******************************************************

1) The page is parsed down to the 1st </script> (before the <body>).
2) The something and isomethingelse are set.
3) sf() is called and the alert works but I get an error on the focus()
statement because "first" hasn't been rendered yet.

"second"

4) Parse more HTML and the onload event is set in the <body> tag - but not
fired yet.
5) The 2nd script section is parsed and as soon as I get to the </script>
the alert is fired.
6) More HTML is parsed, the 2 textboxes are set up.
7) The 3rd script section is parsed and when I get to the </script>, "temp"
is set (so I now have 3 global variables - something, isomethingelse and
temp).
8) sf() is called again, and the alert works as does the focus() since the
the textbox is now there.

I can't say #8 is true for sure. In practice it is commonly true but I
don't know if there is a standard that says that the textbox should be
available to the script at this point. I also don't know if there are
implementations that have contrary behavior. I also don't know if it
is ok by the standards to "focus" before onload.

9) The rest of the page is rendered down to the </html>
10) The onload function of the <body> tag is fired and the alert works here.

onload fires when all HTML has been parsed and all images loaded etc.

11) When I close the alert, I am at the 2nd textbox as expected

Let's hope so.

As I go through the the html the Dom is being built but doesn't get finished
until I get to the </html>

It seems reasonable to assume that the DOM is built "depth first" and
implementation seem to work that way in my experience. I don't know
what the standards say about this.


Is that about right?

As far as I know that is a good interpretation of what happens.

Peter
.



Relevant Pages

  • Re: Functions assigned to variables test
    ... The code in the elements is executed when that HTML element ... rendering is done until it is finished with setting up any global variables ... the alert is fired. ... The onload function of the tag is fired and the alert works here. ...
    (comp.lang.javascript)
  • Re: Functions assigned to variables test
    ... rendering is done until it is finished with setting up any global ... The 2nd script section is parsed and as soon as I get to the ... is ok by the standards to "focus" before onload. ... would be there for the script - but as you say, if there is no standard, I ...
    (comp.lang.javascript)
  • Re: onload ?!!
    ... Alert() as debug thinkg but the alertdoesn't happen when I delete a ... The best onload to use for the whole page is the body's onload event ... Also any script inside script tags that's not in a function will ... in 31kB of html and javascript.) ...
    (comp.lang.javascript)
  • Re: Help with image display please...
    ... I tested with the script below and the onload handler is case ... that alert is triggered in firefox ... function imageLoad() ...
    (comp.lang.javascript)
  • Re: Development with IE 7 for non-Windows users.
    ... standards that most other browsers accept, I rely only on the standards. ... It is only the rendering engine itself that is to blame. ... Sometimes script can be very useful. ... Obviously, if a users browser ...
    (microsoft.public.windows.inetexplorer.ie6.browser)