window.onload seems to trigger before the DOM is loaded (JavaScript)

At the time window is loaded the body isn’t still loaded therefore you should correct your code in the following manner: <script type=”text/javascript”> window.onload = function(){ window.document.body.onload = doThis; // note removed parentheses }; function doThis() { if (document.getElementById(“myParagraph”)) { alert(“It worked!”); } else { alert(“It failed!”); } } </script> Tested to work in FF/IE/Chrome, … Read more

Can I use document.getElementById() with multiple ids?

document.getElementById() only supports one name at a time and only returns a single node not an array of nodes. You have several different options: You could implement your own function that takes multiple ids and returns multiple elements. You could use document.querySelectorAll() that allows you to specify multiple ids in a CSS selector string . … Read more

Mulitple Elements with the same ID

Id must be unique. You should use class instead and then make use of document.getElementsByClassName(‘className’); var video = document.getElementsByClassName(‘vid’); var myFunction = function() { // Reset the video to this.currentTime = 0; // And play again this.load(); }; for (var i = 0; i < video.length; i++) { video[i].addEventListener(‘ended’, myFunction, false); }

getElementById.contentDocument error in IE

The cross-browser equivalent to contentDocument (including Firefox itself, where contentDocument does work) is contentWindow.document. So try: alert(document.getElementById(‘iView’).contentWindow.document); contentWindow gets you a reference to the iframe’s window object, and of course .document is just the DOM Document object for the iframe. Here’s an article that summarizes better.

Javascript Append Child AFTER Element

You can use: if (parentGuest.nextSibling) { parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling); } else { parentGuest.parentNode.appendChild(childGuest); } But as Pavel pointed out, the referenceElement can be null/undefined, and if so, insertBefore behaves just like appendChild. So the following is equivalent to the above: parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);

“getElementById not a function” when trying to parse an AJAX response?

Use DOMParser() to convert responseText into a searchable DOM tree. Also, your attempts to search/use anything derived from responseText, must occur inside the onload function. Use code like this: GM_xmlhttpRequest ( { … onload: parseAJAX_ResponseHTML, … } ); function parseAJAX_ResponseHTML (respObject) { var parser = new DOMParser (); var responseDoc = parser.parseFromString (respObject.responseText, “text/html”); console.log … Read more