简体   繁体   中英

document.onload not working for me?

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('elementhere').innerHTML = 'test';
}
document.onload = test();

It works if I go to the page I want to use it on and do "run" or "reload & run" - but it won't run automatically, which I'm trying to make it do by using document.onload.

What you need is:

window.onload = function () {
// do the work after everything was loaded (DOM, media elements)
}

document.onload it doesn't exist. It works if you at least target some specific element from document for example:

document.querySelector("img").onload = function ()
 {// do your thing after 'img' was loaded}

You have other options if you want to execute some code after the DOM is the only element ready without wait for media elements to load like:

  • Import an async script tag loading the file with the code: You can place this tag wherever you want in DOM and it will not interrupt nothing, will load after the DOM finish to load. you should take a look to MDN documentation about that.

  • If you take a look again in MDN documentation they will recommend you in section Notes to use DOMContentLoaded and DOMFrameContentLoaded events which can be handling by addEventListener. So, if you do this:

document.addEventListener('DOMContentLoaded', handlerFunc);

Will work for you.

I hope this can be helpful for you...

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 is because you are assigning the result of the test function instead of the function itself due to the parentheses () on the last line.
Remove them and it may work.

If not then:

Apparently that is by design. [ Chrome - v57 ]
Use document.addEventListener("DOMContentLoaded", <function_name>); instead.

See: Page lifecycle : DOMContentLoaded

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!

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;
    }

它的工作,但你可以使用替代

window.init = 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.

did you try this?

<body onload="test()">

or

    $(document).ready(function test() {
    document.getElementById('elementhere').innerHTML = 'test';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM