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
|
6 Answers
Reset to default 133Apparently, 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); //
本文标签:
版权声明:本文标题:javascript - Why doesn't document.addEventListener('load', function) work in a greasemonkey script? - St 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1736917447a1956342.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
window
. – Paul S. Commented May 6, 2013 at 17:57window.addEventListener()
? – Yet Another User Commented May 6, 2013 at 17:58window
is much more reliable. – Paul S. Commented May 6, 2013 at 18:13addEventListener
s are being appended towindow
(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 nodocument.addEventListener('load' ...
in the source code :-/ – david.binda Commented Sep 27, 2015 at 18:12