jquery - Why doesn't $('document').load() fire? -
i replaced .ready() .load():
$('document').load(function(){ alert('test'); });
but nothing happens. go ready() , works fine. there isn't in document. it's blank html file.
what doing wrong?
$(document)
not $('document')
when selecting document because there no <document>
element.
additionally, document
doesn't fire load event. window
fires load event. should using $(window).load(...)
.
the reason why $('document').ready()
works because .ready()
doesn't use selector context, , api should have been $.ready(...)
. avoid $(document).ready()
, stick aliasing shorthand version:
jquery(function ($) { ... });
also, aware binding document.ready
after document ready still fire callback. binding window.onload
after window has loaded will not fire callback.
$(document).ready(function () { alert('document selector works'); }); $('document').ready(function () { alert('invalid selector works'); }); $().ready(function () { alert('no selector works'); }); $(window).load(function () { alert('window load works!'); $(window).load(function () { alert("load has fired, doesn't work"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments
Post a Comment