Getting in after window.onload?

388 views
Skip to first unread message

Tony

unread,
May 15, 2007, 4:15:45 PM5/15/07
to greasemonkey-users
I believe I'm having a problem getting my code to execute after code
that is already present in the HTML. However, this is my first
Greasemonkey script, so I may just have no idea what the heck is
happening. On Bioware's forums there is this code snippet:

window.onload = startItUp;

That runs a function that puts focus on the username field in the
login form. I do not want that. So I wrote a one-liner:

window.addEventListener('load', function()
{ document.getElementById('username').blur(); }, false);

No errors appear in the Javascript console. But the focus remains on
the username field. I guess this is because the Greasemonkey script
is being executed right before the "window.onload" action. Is my
guess correct? What to do?

RodMcguire

unread,
May 15, 2007, 5:34:45 PM5/15/07
to greasemonkey-users
On May 15, 4:15 pm, Tony <goo...@outshine.com> wrote:
> window.addEventListener('load', function()
> { document.getElementById('username').blur(); }, false);
>
> No errors appear in the Javascript console. But the focus remains on
> the username field. I guess this is because the Greasemonkey script
> is being executed right before the "window.onload" action. Is my
> guess correct? What to do?

window.addEventListener('load', function(){
setTimeout(function ()
{document.getElementById('username').blur(); }, 10);
},
false);

instead. If you diagnosed your problem correctly, this will wait 10 ms
before doing the blur. Since Firefox is single threaded, this will
insure that everything triggered by a load event will run first.

Tony

unread,
May 15, 2007, 5:57:21 PM5/15/07
to greasemonkey-users
It worked! I forgot that setTimeout doesn't hold up the code
execution, so of course it enables something to execute out of order.
Thank you very much!

-Tony

Christian Blackburn

unread,
May 15, 2007, 8:31:02 PM5/15/07
to greasemonkey-users
Hi Guys,

Is there some reason one should use a timeout? I'm just populating
the user name and password before the onLoad event, adding an event
trigger and then submitting the form. I get that this doesn't allow
for the browser to autopopulate the login text boxes, but is there
some other good reason to use timeouts?

document.forms[0].elements.namedItem("code").addEventListener("focus",
LiveActions(), false)

function LiveActions()
{
document.forms[0].submit();
}


Thanks,
Christian Blackburn

RodMcguire

unread,
May 15, 2007, 8:55:56 PM5/15/07
to greasemonkey-users
On May 15, 8:31 pm, Christian Blackburn

<christian.Blackb...@Yahoo.com> wrote:
> Is there some reason one should use a timeout? I'm just populating
> the user name and password before the onLoad event, adding an event
> trigger and then submitting the form. I get that this doesn't allow
> for the browser to autopopulate the login text boxes, but is there
> some other good reason to use timeouts?
>
> document.forms[0].elements.namedItem("code").addEventListener("focus",
> LiveActions(), false)
>

Tony had to use a timeout delay because I guess some "load" event was
triggering code to set the focus. If your code works without a
timeout then you don't need one.

BTW, I hope your real code has LiveActions not LiveActions()".

Christian Blackburn

unread,
May 15, 2007, 11:12:58 PM5/15/07
to greasemonkey-users
Hi Rod,

My real code doesn't have LiveActions, should it be quoted
("LiveActions")? It seems to work :). However, I can't get it to
work on one page, but it works on another with the same exact
syntax.

Cheers,
Christian

RodMcguire

unread,
May 15, 2007, 11:33:04 PM5/15/07
to greasemonkey-users
On May 15, 11:12 pm, Christian Blackburn

<christian.Blackb...@Yahoo.com> wrote:
> My real code doesn't have LiveActions, should it be quoted
> ("LiveActions")? It seems to work :). However, I can't get it to
> work on one page, but it works on another with the same exact
> syntax.

Please read "Avoid Common Pitfalls in Greasemonkey", especially
"Pitfall #1: Auto-eval Strings".

http://www.oreillynet.com/lpt/a/6257

Reply all
Reply to author
Forward
0 new messages