document.getElementById not returning elements on initial page load

983 views
Skip to first unread message

Simon

unread,
Aug 3, 2009, 9:09:57 AM8/3/09
to greasemonkey-users
Hey,

I was wondering if someone could help me. I've written a small script
to fill in a form which I have to do a lot for me work.

I'm getting something strange happening where the script does not work
when I first load the page. If I refresh the page then the script
works and the form is filled as expected.

The page doesn't change upon requests. What is more, I can inspect
the source and the DOM and the element which is reported as being null
(document.getElementById is returning it null) is there.

I can also inspect the page via a Javascript shell in that page's
context. Running document.getElementById in the shell returns the
element correctly.

Has anyone ideas for what this may be?

Madepally Kiran

unread,
Aug 3, 2009, 10:15:37 AM8/3/09
to greasemon...@googlegroups.com
Hi, may be the element which you are requesting is being created dynamically or before it is being used in the GM script. You may try retrying with a little delay till the element is returned not null. In case of refresh, the page may be cached and loaded quickly so that the element already exists before it is being called for.

Simon

unread,
Aug 3, 2009, 10:37:22 AM8/3/09
to greasemonkey-users
Hey,

Thanks for the reply. Its definitely not created dynamically. I'm a
developer on the site the script runs on and its all static HTML. The
refresh thing works like clockwork, as in, without fail the script
fails the first time and works all successive times. Very strange.

On Aug 3, 3:15 pm, Madepally Kiran <the.phoen...@gmail.com> wrote:
> Hi, may be the element which you are requesting is being created dynamically
> or before it is being used in the GM script. You may try retrying with a
> little delay till the element is returned not null. In case of refresh, the
> page may be cached and loaded quickly so that the element already exists
> before it is being called for.
>

Madepally Kiran

unread,
Aug 3, 2009, 3:08:54 PM8/3/09
to greasemon...@googlegroups.com
WOW, that's strange. If the HTML is completely static, then there is no reason why it shouldnt work the first time itself! Can I have a look at the page and the part of the script that is causing the errors? May be I could be of more help then.
Reply all
Reply to author
Forward
0 new messages