473,385 Members | 2,180 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,385 software developers and data experts.

DOM construction and script execution

Hi,

In the following snip of and HTML page, is there a specification
guarantee that the foo element will be parsed and available as part of
the DOM by the time the script executes? That is, could this error in a
standards-compliant browser?

<p id="foo">foo</p>

<script type="text/javascript>
var foo = document.getElementById('foo');
</script>

This seems to be very common practice. If this is not a standard then
perhaps this is a de facto standard that all browsers honour?

Thank you,
Peter

Jan 27 '07 #1
15 1377
Peter Michaux wrote on 27 jan 2007 in comp.lang.javascript:
In the following snip of and HTML page, is there a specification
guarantee that the foo element will be parsed and available as part of
the DOM by the time the script executes? That is, could this error in a
standards-compliant browser?
Methinks there are NO standards-compliant browsers yet.
There never will be,
as [long as] standards are something to aim at.
<p id="foo">foo</p>

<script type="text/javascript>
var foo = document.getElementById('foo');
</script>

This seems to be very common practice. If this is not a standard then
perhaps this is a de facto standard ...
It is not an standars per se.

foo will not be part of the DOM,
but is a javascript variable pointing to a DOM element,
[perceivesd by javascript as being an object, I think].

.... that all browsers honour?

No, not "all".
There always will be browsers, like very old versions, that will not,
but be assured that most modern ones and most ones audience volume wize do.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 27 '07 #2
On Jan 27, 1:06 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
<p id="foo">foo</p>
<script type="text/javascript>
var foo = document.getElementById('foo');
</script>
This seems to be very common practice. If this is not a standard then
perhaps this is a de facto standard ...It is not an standars per se.

foo will not be part of the DOM,
but is a javascript variable pointing to a DOM element,
[perceivesd by javascript as being an object, I think].
Let me make that more clear with different strings.

<p id="myPara">asdf</p>

<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

So excluding browsers before IE5 and NN6 or without
document.getElementById, does anyone know of a browser that will error
when this JavaScript runs or that will not return the "myPara" element
when document.getElementById() is called?

Thanks,
Peter

Jan 27 '07 #3
Peter Michaux wrote on 27 jan 2007 in comp.lang.javascript:
On Jan 27, 1:06 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
<p id="foo">foo</p>
<script type="text/javascript>
var foo = document.getElementById('foo');
</script>
This seems to be very common practice. If this is not a standard
then perhaps this is a de facto standard ...It is not an standars
per se.

foo will not be part of the DOM,
but is a javascript variable pointing to a DOM element,
[perceivesd by javascript as being an object, I think].

Let me make that more clear with different strings.

<p id="myPara">asdf</p>

<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

So excluding browsers before IE5 and NN6 or without
document.getElementById, does anyone know of a browser that will error
when this JavaScript runs or that will not return the "myPara" element
when document.getElementById() is called?
If you expect those old browser to be used occasionally,
you could test for the functionality:

<p id="myPara">asdf</p>

<script type="text/javascript>
if (document.getElementById('myPara')){
var foo = document.getElementById('myPara');

// rest of code

}
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 27 '07 #4
On Jan 27, 10:52 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
Peter Michaux wrote on 27 jan 2007 in comp.lang.javascript:

<p id="myPara">asdf</p>

<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>
So excluding browsers before IE5 and NN6 or without
document.getElementById, does anyone know of a browser that will error
when this JavaScript runs or that will not return the "myPara" element
when document.getElementById() is called?

If you expect those old browser to be used occasionally,
you could test for the functionality:

<p id="myPara">asdf</p>

<script type="text/javascript>
if (document.getElementById('myPara')){
var foo = document.getElementById('myPara');

// rest of code

}
</script>
Thanks for the response but I think you've missed my question a
little. What I am trying to determine is if there is a de facto
standard behavior or if anyone has actually witnessed a browser where
my code snip would fail to perform (minus the obvious case where
document.getElementById() is not supported).

Peter

Jan 27 '07 #5
VK
What I am trying to determine is if there is a de facto
standard behavior
No, it is a non-standard behavior added by many browser producers
against DOM specs and own producers statements. This question was
posed a number of times, a couple of years ago I answered with quotes
from w3.org and MSDN - but I cannot find this post anymore, have to
search these dumpsters once over - some later. You cannot depend on
this behavior neither expect some consistent documented behavior
across UAs.

"Genetically" this behavior extension is of the same kind as
window.location.href(URL) added on IE - thus location used as a method
and not as a property. On a bigger scale it is another reflection of
JavaScript programming specifics where a great amount of developers
are not programmers as such. Respectively traditionally a great amount
of people programming on JavaScript have their own - sometimes very
strong - ideas of what and how should act and consecutively not giving
any sh** of how should it work by some stupid specs. At some point
this or that UA producer decides to save some nerves so simply adding
the most common mis-usages as extra features.

<http://groups.google.com/group/comp....avascript/msg/
b6e5bfd65e6813af>
<quote>
Once again, for any robust solution the mantra is:

before page load event - document.write only
after page load event - DOM methods only
Ommm...
....
before page load event - document.write only
after page load event - DOM methods only
Ommm...
....

Repeat 10 times before go to bed and the life becomes much more easy
and your customers will stay happy, and money will stay in your house.

:-)
</quote>

Anyone is welcome to live by this VK's mantra or call instead to the
spirit of good luck.

Jan 27 '07 #6
dd
I remember when this wouldn't work on some browsers,
I'm fairly sure it was limited to Netscape pre 6:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
var foo = document.getElementById('myPara');
</script>

That would fail because (I summized) that the document
writes were being buffered until the script tag closed. As
I recall (and it was a few years back so I might be fuzzy),
this solved the problem:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
</script>
<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

Closing and then re-opening the script tags caused the
pending document.writes to get put into the DOM. This
isn't what you're dealing with but I thought it might be
interesting to bring up.

Jan 28 '07 #7
dd wrote on 28 jan 2007 in comp.lang.javascript:
I remember when this wouldn't work on some browsers,
What is "this"?

[please always quote on usenet]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 28 '07 #8
dd
On Jan 28, 10:07 am, "Evertjan." <exjxw.hannivo...@interxnl.net>
wrote:
dd wrote on 28 jan 2007 in comp.lang.javascript:
I remember when this wouldn't work on some browsers,What is "this"?
In my post I wasn't quoting anything. By 'this' I meant
'this code below', indicated by the colon at the end of
the line :)

~dd

Jan 28 '07 #9
Hi dd,

On Jan 28, 1:01 am, "dd" <dd4...@gmail.comwrote:
I remember when this wouldn't work on some browsers,
I'm fairly sure it was limited to Netscape pre 6:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
var foo = document.getElementById('myPara');
</script>

That would fail because (I summized) that the document
writes were being buffered until the script tag closed.
I saw this very technique used just a few days ago and it made be
wonder what is going on. It seems like this script shouldn't work even
today. Very forgiving of the browser makers to enable this script.

>As
I recall (and it was a few years back so I might be fuzzy),
this solved the problem:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
</script>
<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

Closing and then re-opening the script tags caused the
pending document.writes to get put into the DOM. This
isn't what you're dealing with but I thought it might be
interesting to bring up.
Definitely good to know. Much appreciated.

Thanks,
Peter

Jan 28 '07 #10
"dd" <dd****@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
>I remember when this wouldn't work on some browsers,
I'm fairly sure it was limited to Netscape pre 6:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
var foo = document.getElementById('myPara');
</script>

That would fail because (I summized) that the document
writes were being buffered until the script tag closed. As
I recall (and it was a few years back so I might be fuzzy),
this solved the problem:

<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
</script>
<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

Closing and then re-opening the script tags caused the
pending document.writes to get put into the DOM. This
isn't what you're dealing with but I thought it might be
interesting to bring up.
I wonder then, is this behavior that we should rely on? The first example worked in
Internet Explorer 6, Firefox 1.5.0.9, and Opera 9.10.

In my opinion that is "definitive" enough to warrant it being reliable, unless of course
some document somewhere states that it specifically needs to be removed.

-Lost
Jan 28 '07 #11
Peter Michaux wrote:
<snip>
Let me make that more clear with different strings.

<p id="myPara">asdf</p>

<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

So excluding browsers before IE5 and NN6 or without
document.getElementById, does anyone know of a browser
that will error when this JavaScript runs or that will
not return the "myPara" element when document.getElementById()
is called?
I am not aware of any browser where an attempt to retrieve a reference to
an element from the DOM would fail following the HTML parser's parsing of
the element's mark-up (indeed the parsing of the opening tag would appear
to be sufficient).

However, what you can do with that reference, and what you can expect
from it, is extremely questionable. For example, your chances of getting
useful (or any) positioning and size information from the element at that
point are quite low.

Richard.

Jan 28 '07 #12
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...
Peter Michaux wrote:
<snip>
>Let me make that more clear with different strings.

<p id="myPara">asdf</p>

<script type="text/javascript>
var foo = document.getElementById('myPara');
</script>

So excluding browsers before IE5 and NN6 or without
document.getElementById, does anyone know of a browser
that will error when this JavaScript runs or that will
not return the "myPara" element when document.getElementById()
is called?

I am not aware of any browser where an attempt to retrieve a reference to an element
from the DOM would fail following the HTML parser's parsing of the element's mark-up
(indeed the parsing of the opening tag would appear to be sufficient).

However, what you can do with that reference, and what you can expect from it, is
extremely questionable. For example, your chances of getting useful (or any) positioning
and size information from the element at that point are quite low.
Is it possible for you to provide an example of this theory?

I created one or two scenarios dealing with IMGs. Both retrieving their dimensions and
their position in the same context as I created them. So maybe I misinterpreted something
or am simply not enacting the correct tests.

-Lost
Jan 29 '07 #13
-Lost wrote:
Richard Cornford wrote:
<snip>
>However, what you can do with that reference, and what you can expect
from it, is extremely questionable. For example, your chances of
getting useful (or any) positioning and size information from the
element at that point are quite low.

Is it possible for you to provide an example of this theory?
It is not a theory. Try reading the - offsetWidth - properties of various
Elements in various browsers before the browser has finished parsing the
whole DOM and you will find some that have not assign values to those
properties (and some that have assigned provisional values during
progressive rendering, but those values may change when the whole DOM is
laid out).
I created one or two scenarios dealing with IMGs. Both retrieving
their dimensions and their position in the same context as I
created them. So maybe I misinterpreted something or am simply
not enacting the correct tests.
How many versions of how many browsers, with how many types of elements,
have you tried it with? And using what code?

Richard.

Jan 29 '07 #14
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:ep*******************@news.demon.co.uk...
-Lost wrote:
>Richard Cornford wrote:
<snip>
>>However, what you can do with that reference, and what you can expect
from it, is extremely questionable. For example, your chances of
getting useful (or any) positioning and size information from the
element at that point are quite low.

Is it possible for you to provide an example of this theory?

It is not a theory. Try reading the - offsetWidth - properties of various Elements in
various browsers before the browser has finished parsing the whole DOM and you will find
some that have not assign values to those properties (and some that have assigned
provisional values during progressive rendering, but those values may change when the
whole DOM is laid out).
I suppose I should have said implementation, not theory. Anyway...

Well, initially we were talking about positioning and size information. (And I assumed
"size information" meant only a width or height property, no offsets.) I used the:
<script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
var foo = document.getElementById('myPara');
</script>
....approach. In this fashion I was able to retrieve all attributes I set. Which included
inline styles, id, alt, width, height et cetera.
How many versions of how many browsers, with how many types of elements, have you tried
it with? And using what code?
I only tested in Internet Explorer 6, Opera 9.10, and Firefox 1.5.0.9.

After modifying this to a DOM-only representation, I was unable to retrieve valid offsets.
So, I understand now. Thanks.

-Lost
Jan 29 '07 #15
"-Lost" <sp****************@REMOVEMEcomcast.netwrote in message
news:nr******************************@comcast.com. ..
>It is not a theory. Try reading the - offsetWidth - properties of various Elements in
various browsers before the browser has finished parsing the whole DOM and you will
find some that have not assign values to those properties (and some that have assigned
provisional values during progressive rendering, but those values may change when the
whole DOM is laid out).

I suppose I should have said implementation, not theory. Anyway...

Well, initially we were talking about positioning and size information. (And I assumed
"size information" meant only a width or height property, no offsets.) I used the:
><script type="text/javascript>
document.write('<p id="myPara">asdf</p>');
var foo = document.getElementById('myPara');
</script>

...approach. In this fashion I was able to retrieve all attributes I set. Which
included inline styles, id, alt, width, height et cetera.
>How many versions of how many browsers, with how many types of elements, have you tried
it with? And using what code?

I only tested in Internet Explorer 6, Opera 9.10, and Firefox 1.5.0.9.

After modifying this to a DOM-only representation, I was unable to retrieve valid
offsets. So, I understand now. Thanks.
OK, I made a mistake. I was setting the width and height to '32px', not 32. When I
initially had that offsetWidth gave me *very* strange results.

var body_obj = document.getElementsByTagName('body')[0];
var img_obj = document.createElement('img');
img_obj.id = 'img1';
img_obj.src = 'loading.gif';
img_obj.alt = 'loading...';
img_obj.width = 32;
img_obj.height = 32;
body_obj.appendChild(img_obj);
var img = document.getElementById('img1');
document.write('<br />' + img.offsetWidth);
document.write('<br />' + img.width);

Once I changed width and height, offsetWidth and width were both available and accurate
with or without me explicitly setting the width and height attributes.

I call that directly within the BODY and in one SCRIPT context. I may have done this
incorrectly or may not understand if the HTML DOM has finished loading, but I thought it
was accurate. For lack of better understanding...

Thanks.

-Lost
Jan 29 '07 #16

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

Similar topics

10
by: Doug | last post by:
I have a script that does a huge database update, which takes a long time. However, the host I am running on (and I suspect most hosts) are running in "safe mode." This means I cannot change the...
5
by: Boris Nikolaevich | last post by:
This is backwards of what I usually want--normally if you have a long-running ASP script, it's a good idea to check to see whether the client is still connected so you can cancel execution. ...
3
by: #pragma once | last post by:
That's all we are expecting from programs written in the managed code; Though a MVP advised not to say that, because after JIT compilation the code runs in the native! Funny, isn't? That means...
14
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling...
29
by: Merrill & Michele | last post by:
I'm now looking at page 115 K&R. After having discussed counterexamples, I shall herewith and henceforth make all my c programs look like int main(int orange, char* apple) {return(0);} Q1) ...
3
by: iam980 | last post by:
Hello All. We have tested following SQL script from query analyzer: -- Script begin DECLARE @I int; SET @I = 1; WHILE @I < 10000000 BEGIN SET @I = @I + 1; END -- Script end
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
2
by: Paul | last post by:
I have a PHP script that is triggered by a .forward file when incoming email arrives. When triggered, the script retrieves the email using the php imap functions, processes the email, and then...
5
by: Guillermo Antonio Amaral Bastidas | last post by:
Hi everybody, I have a quick and probably dumb question, keep in mind I just dumped my old love FastCGI + Perl for it's younger hotter friend PHP5. If the user calls a time consuming script...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.