How to imitate cross-browser $(document).ready() behavior without JQuery [duplicate]

℡╲_俬逩灬. 提交于 2019-12-11 02:54:59

问题


Possible Duplicate:
$(document).ready equivalent without jQuery

I have a script I need to write that needs to execute on DOM ready, but I can't have dependency on JQuery as part of the script.

I want to imitate $(document).ready(), How can I implement this behavior in the shortest way?


回答1:


You can use:

document.addEventListener("DOMContentLoaded", function(){
//do things
},false);

for example. Note that this might be a problem in older browsers though, see MDN for a list of supported browsers.




回答2:


You could do 2 things. On the body tag add an onload event:

    <body onload="myJSFunction()"></body>

Otherwise, you can add the functions in the bottom of the markup with:

<script type="text/javascript"> function myFunction()</script>



回答3:


window.onload = function() {
    // write some javascript here
    // this will be executed on page load
}


来源:https://stackoverflow.com/questions/9784346/how-to-imitate-cross-browser-document-ready-behavior-without-jquery

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