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?
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
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
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()".
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
Please read "Avoid Common Pitfalls in Greasemonkey", especially
"Pitfall #1: Auto-eval Strings".