[removed] not working for me?

前端 未结 8 1943
栀梦
栀梦 2021-02-07 14:17

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(\'         


        
相关标签:
8条回答
  • 2021-02-07 14:43

    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.

    0 讨论(0)
  • 2021-02-07 14:44

    It's working but you can use alternate

    window.init = test;
    
    0 讨论(0)
  • 2021-02-07 14:47

    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!

    0 讨论(0)
  • 2021-02-07 14:49

    did you try this?

    <body onload="test()">
    

    or

        $(document).ready(function test() {
        document.getElementById('elementhere').innerHTML = 'test';
        }
    
    0 讨论(0)
  • 2021-02-07 14:54

    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.

    0 讨论(0)
  • 2021-02-07 15:00

    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;
        }
    
    0 讨论(0)
提交回复
热议问题