我在页面加载后使用以下代码执行一些语句。
I am using following code to execute some statements after page load.
<script type="text/javascript"> window.onload = function () { newInvite(); document.ag.src="b.jpg"; } </script>但此代码无法正常运行。即使正在加载某些图像或元素,也会调用该函数。我想要的是调用该函数完全加载页面。
But this code does not work properly. The function is called even if some images or elements are loading. What I want is to call the function the the page is loaded completely.
推荐答案这可能适合你:
document.addEventListener('DOMContentLoaded', function() { // your code here }, false);或如果您对jquery感到满意,
or if your comfort with jquery,
$(document).ready(function(){ // your code });$(document).ready()在DOMContentLoaded上触发,但是在浏览器中不会一致地触发此事件。这就是jQuery最有可能实现一些重要的解决方法来支持所有浏览器的原因。这将使得使用普通Javascript完全模拟行为变得非常困难(当然不是不可能)。
$(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers. This is why jQuery will most probably implement some heavy workarounds to support all the browsers. And this will make it very difficult to "exactly" simulate the behavior using plain Javascript (but not impossible of course).
正如Jeffrey Sweeney和J Torres建议的那样,我认为在触发如下函数之前,最好有一个 setTimeout 函数:
as Jeffrey Sweeney and J Torres suggested, i think its better to have a setTimeout function, before firing the function like below :
setTimeout(function(){ //your code here }, 3000);更多推荐
完成页面加载后执行函数
发布评论