Re: Functions assigned to variables test
- From: Peter Michaux <petermichaux@xxxxxxxxx>
- Date: Sat, 22 Mar 2008 21:18:25 -0700 (PDT)
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
.
- Follow-Ups:
- Re: Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- References:
- Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- From: RobG
- Re: Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- From: tshad
- Re: Functions assigned to variables test
- From: Peter Michaux
- Re: Functions assigned to variables test
- From: tshad
- Functions assigned to variables test
- Prev by Date: Re: Page Session Countdown Timer
- Next by Date: Re: Why is 0 == "" in JavaScript?
- Previous by thread: Re: Functions assigned to variables test
- Next by thread: Re: Functions assigned to variables test
- Index(es):
Relevant Pages
|