Alternative for “$(document).ready” function

爷,独闯天下 提交于 2020-03-18 10:28:51

问题


I am using fancybox in an aspx page. The document ready function does not work in this page for a lightbox. Someone told me to write a new javascript code for loading the lightbox in that page.


回答1:


  • Include jQuery.
  • Check network tab that you are not getting 404.
  • Check console that you are not getting "$ is unknown".

Do stuff when DOM is ready.

$(function(){
   // DOM Ready - do your stuff 
});



回答2:


Try this:

document.addEventListener('DOMContentLoaded', function() {
   // ...
});

Works in modern browers and IE9+




回答3:


You could use the standard js onload function to run if that's what your'e missing:

window.onload = function() {};

Do note that this might give you issues with libraries - I haven't investigated that.




回答4:


best ways is to use like this:

jQuery.noConflict();
(function($) {
  $(function() {
   // by passing the $ you can code using the $ alias for jQuery
   alert('Page: ' + $('title').html() + ' dom loaded!');
  });
})(jQuery);



回答5:


I believe using the script defer tag is the best solution. For example,

<script src="demo_defer.js" defer></script>

More information at W3 Schools.



来源:https://stackoverflow.com/questions/12366410/alternative-for-document-ready-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!