admin管理员组

文章数量:1135147

It doesn't give an error, and I put a console.log('loaded userscript wifi-autologin'), the console.log works, but the intended effect of the document.addEventListener doesn't happen. After doing a bit more debugging, making it print that the addEventListener was called, I discovered that it wasn't being called.

Source of script:

// ==UserScript==
// @name        wifi-autologin
// @namespace   lf-ns
// @description Hopefully autologins to a captive portal
// @include     *://1.1.1.1/*
// @version     1
// @run-at document-end
// ==/UserScript==

document.addEventListener('load', submitAction);

It doesn't give an error, and I put a console.log('loaded userscript wifi-autologin'), the console.log works, but the intended effect of the document.addEventListener doesn't happen. After doing a bit more debugging, making it print that the addEventListener was called, I discovered that it wasn't being called.

Source of script:

// ==UserScript==
// @name        wifi-autologin
// @namespace   lf-ns
// @description Hopefully autologins to a captive portal
// @include     *://1.1.1.1/*
// @version     1
// @run-at document-end
// ==/UserScript==

document.addEventListener('load', submitAction);
Share Improve this question edited Aug 8, 2017 at 18:47 kenorb 166k94 gold badges707 silver badges775 bronze badges asked May 6, 2013 at 17:56 Yet Another UserYet Another User 2,8853 gold badges19 silver badges28 bronze badges 5
  • 15 Listen for the load event on window. – Paul S. Commented May 6, 2013 at 17:57
  • 3 You mean window.addEventListener()? – Yet Another User Commented May 6, 2013 at 17:58
  • Yep, although both work ( see jsbin.com/aqoweb/1 ), I find using window is much more reliable. – Paul S. Commented May 6, 2013 at 18:13
  • 1 @PaulS. From what I can see in the pastebin you linked, all addEventListeners are being appended to window (even the one which is meant to be hooked to document as the comment in the code and the message in the console says). There's no document.addEventListener('load' ... in the source code :-/ – david.binda Commented Sep 27, 2015 at 18:12
  • I agree @david.binda, the example of Paul S. does not test document event handlers. just window ones. – Alex MM Commented Oct 22, 2015 at 13:17
Add a comment  | 

6 Answers 6

Reset to default 133

Apparently, document.addEventListener() is unreliable, and hence, my error. Use window.addEventListener() with the same parameters, instead.

To get the value of my drop down box on page load, I use

document.addEventListener('DOMContentLoaded',fnName);

Hope this helps some one.

According to HTML living standard specification, the load event is

Fired at the Window when the document has finished loading; fired at an element containing a resource (e.g. img, embed) when its resource has finished loading

I.e. load event is not fired on document object.

Credit: Why does document.addEventListener(‘load’, handler) not work?

For rookies like me, the top voted answer is not correct. document.addEventListener() is not perfect but it is reliable.

The simple answer document.addEventListener('load', function) doesn't work is because the load event fires on the window, not the document.

See the documentation here - https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event

window.addEventListener('load', runsWhenReady); //✅ load fires on the window and works.

document.addEventListener('DOMContentLoaded', runsWhenReady); //✅ domcontent fires on the document and works

document.addEventListener('load', runsWhenReady); //

本文标签: