addOnLoad() should cause the function to be executed as soon as the DOM
is complete, which may be before the page has finished loading and
rendering. However, since the DOM will be fully constructed,
getElementById() should work properly at that point. I'd go with Dave's
advice and confirm that the element you're referencing really exists in
the DOM. And, if you really do have an element with an ID of 'name[0]',
I'd definitely try changing the ID to something that's legal, in case
that's what's confusing the browser.
L.
Pablo Vázquez Blázquez wrote:
<script type="text/javascript">
function init() {
alert(document.getElementById('name[0]'));
}
dojo.addOnLoad(init);
</script>
It is the same as:
<script type="text/javascript">
alert(document.getElementById('name[0]'));
</script>
It is executed before the page loads.
Dave Newton escribió:
--- Pablo Vázquez Blázquez <[EMAIL PROTECTED]> wrote:
Anyone knows why my javascript code is executed at the loading of a
page instead when it *should*??
Mostly because of your definition of "should" ;)
For example, I have a .jspx page with a form:
<form>
...
<input id="id" ...>
...
<form>
<script>
alert('hi');
alert(document.getElementById('id'));
</script>
'hi' is shown before the form and then 'undefined'.
But my js code is at the end of the page. How can it be possible?
Just because your JavaScript is at the end of the page doesn't mean the
entire page (thus the DOM) will be rendered before your JavaScript
executes,
and it may be browser-dependent.
As was pointed out you can put your JavaScript in a window.onload
section;
IIRC this will wait until *everything* has loaded, including images etc.
However, putting such JavaScript in an "window.onload" handler *may*
interfere with Dojo's own onload handler, which may do DOM rewriting
and so
on.
Instead try using Dojo's:
dojo.addOnLoad(aFunction)
where "aFunction" is either a function reference or an anonymous
function.
See dojo.addOnLoad [1] and a short blog post [2] for some further
information.
Dave
[1] Dojo's dojo.addOnLoad function:
http://redesign.dojotoolkit.org/jsdoc/dojo/HEAD/dojo.addOnLoad
[2]
http://www.dev411.com/blog/2006/07/13/dojo-dojo-addonload-vs-body-onload-and-window-onload
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]