views:

1033

answers:

3

i am trying to appendChild() on an existing form, and its not working. i wonder if i need to delay the page, at least i thought i read that somewhere. what i am thinking is dynamically altering window onload to be a delay. do i grab the body tag like any other DOM element?

+1  A: 

you could use a timeout to delay the call to your method that does the appendChild()...

setTimeout(functionName, "200")

If you are using jQuery you can use the ready method to delay your code until the page is fully loaded:

$(document).ready(function() {
    appendChild();
});

Edit: Removed quotes around function call in setTimeout per Steve's suggestion in the comments

Jimmie R. Houts
Don't quote the function in "setTimeout", as this calls "eval()". Rather, provide a function reference: "setTimeout(functionName, 200);".
Steve Harrison
don't answer with jQuery unless the asker says to. just because a library makes it easier does not mean they are using it.
geowa4
@Steve can you provide a reference for your statement? I have not come accross that before.
Jimmie R. Houts
@George IV I provided an adequete sample in the first part of my answer to address the question. However, I do not see the harm in showing an additional jQuery sample, especially when using the jQuery solution is better than guessing how long the timeout needs to be.
Jimmie R. Houts
@Jimmie R. Houts: See http://javascript.crockford.com/code.html and https://developer.mozilla.org/en/DOM/window.setTimeout#Syntax.
Steve Harrison
@Steve Thanks for the links, while searching for more info on this, I came accross this page which does a good job of explaining it: http://dev.opera.com/articles/view/efficient-javascript/?page=2
Jimmie R. Houts
@Jimmie R. Houts: That's a great page you found! I've added it to my bookmarks!
Steve Harrison
@Jimmie R. Houts: You also need to remove the parentheses. Now, instead of passing code to be executed (as you did when quoting it), you're passing a "function reference" (i.e. you're passing a variable that refers to a function).
Steve Harrison
@Steve my bad, didn't mean to leave those on there
Jimmie R. Houts
A: 

the body-element is referenced directly under document,

document.body

But your question is very fuzzy.

If you call your DOM-manipulation from the onload-event, the DOM should be completely loaded. Therefor I assume a delay isn't the way to go.

jishi
+2  A: 

Does this work?

window.addEventListener('load', function() {
    // Code to execute when DOM is loaded
}, false);

Steve

Steve Harrison
newbie js question...when programming js, you must rely on memory about attributes and functions, there is not a tool that gives code assist?
Of course, memory helps (as it always does), but there are plenty of reference libraries out there. I particularly recommend the Mozilla Developer Centre (https://developer.mozilla.org/en/JavaScript). W3Schools is also good (http://www.w3schools.com/js/default.asp).
Steve Harrison
are there any IDEs that are integrated, to get code assist like help?
I'm not sure. Try searching StackOverflow (http://stackoverflow.com/questions/tagged/ide+javascript might be of help), and if you don't find anything, perhaps start an new question.
Steve Harrison