views:

45

answers:

1

Hi.

I'm Trying to access results div in the results page of google.co.uk. Using firebug one can see that the id of the div is "res" but for some reason getElementById('res') fails. to make things even weirder - if i refresh the page (F5 or ctrl+F5), the function succeeds.

Also, if i look in the source code of the results page i dont see anything that looks like the DOM described in firebug.

Why is this happening and how can i ensure getElementById('res') will succeed with any refresh by the user.

Thanks.

EDIT: im adding a a short code to simplify the problem. after placing a query in google.co.uk the page redirects and the alert 'working' pops but the second alert doesnt. after refreshing, both alerts pop although the second one says 0 which is not right because the div has children according to the firebug DOM.

p.s: i also failed to mention that im using greasmonkey

(function() {
alert('working');
var results = document.getElementById('res');
alert(results.childNodes.length);

})();

A: 
window.addEventListener("DOMContentLoaded", function () {
    var results = document.getElementById('res');
    alert(results.childNodes.length);
}, false);
AutoSponge
thanks for answering. i've tried you solution and i get "results is null" error. i think that adding this event listener is even worse because it is fired when the DOM structure is complete but not necessarily after all the page is loaded including images and scripts
Ari