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
2009-04-16 21:41:40
Don't quote the function in "setTimeout", as this calls "eval()". Rather, provide a function reference: "setTimeout(functionName, 200);".
Steve Harrison
2009-04-16 21:43:48
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
2009-04-16 21:45:22
@Steve can you provide a reference for your statement? I have not come accross that before.
Jimmie R. Houts
2009-04-16 21:48:51
@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
2009-04-16 21:59:15
@Jimmie R. Houts: See http://javascript.crockford.com/code.html and https://developer.mozilla.org/en/DOM/window.setTimeout#Syntax.
Steve Harrison
2009-04-16 22:05:11
@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
2009-04-16 22:12:12
@Jimmie R. Houts: That's a great page you found! I've added it to my bookmarks!
Steve Harrison
2009-04-16 22:15:51
@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
2009-04-16 22:20:38
@Steve my bad, didn't mean to leave those on there
Jimmie R. Houts
2009-04-16 22:30:21
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
2009-04-16 21:41:40
+2
A:
Does this work?
window.addEventListener('load', function() {
// Code to execute when DOM is loaded
}, false);
Steve
Steve Harrison
2009-04-16 21:42:11
newbie js question...when programming js, you must rely on memory about attributes and functions, there is not a tool that gives code assist?
2009-04-16 22:12:14
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
2009-04-16 22:27:33
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
2009-04-16 22:58:10