Well I\'m simply playing around with a userscript of GreaseMonkey and here\'s just something simple I attempt to do;
function test() {
document.getElementById(\'
When you write document.onload = test()
, you are calling the test
function, and assigning its return value to the onload
handler. This is clearly not what you intended.
document.onload = test;
This will assign a reference to the test
function to the onload
handler. When the event fires, that is when your function will be called.
It's working but you can use alternate
window.init = test;
Maybe do this:
<body onload="test()">
Or is that not what you're looking for? If you want to do it without the HTML bit above, maybe try and replace document with window:
window.onload = function ()
{
document.getElementById('elementhere').innerHTML = 'test';
}
I would give that a go because of this:
window.onload vs document.onload
I know that posting a link to a link is lame, but you will see why I did that (hopefully).
Cheers!
did you try this?
<body onload="test()">
or
$(document).ready(function test() {
document.getElementById('elementhere').innerHTML = 'test';
}
If you are using Greasemonkey then you don't really need a using onload event since the Greasemoneky script is called only when DOMContentLoaded
is fired.
The code in a Greasemonkey user script gets invoked when the DOMContentLoaded event fires (during event capture as of GM 0.8.6). It is a DOM event implemented by Mozilla, similar to window.onload. However, since it waits only for the DOM to load, instead of the entire page (including images, style sheets, and etc.) it happens sooner.
http://wiki.greasespot.net/DOMContentLoaded
I think you would just need to call test()
without any event listeners.
No Need to call the function on the page .. just you need to use below code which is best practices of coading
window.onload = function() {
document.getElementById("txtCaptcha").value = code;
document.getElementById("txtCaptchaDiv").innerHTML = code;
}