在完成页面加载后调用一个函数 [英] call a function after complete page load

查看:108
本文介绍了在完成页面加载后调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < script type =text / javascript > 
window.onload = function(){

newInvite();
document.ag.src =b.jpg;
}
< / script>

但是这段代码无法正常工作。即使某些图像或元素正在加载,该函数也会被调用。我想要的是调用页面完全加载的函数。

解决方案

这可能适合您:


$ b $ pre $ document.addEventListener('DOMContentLoaded',function(){
//你的代码在这里
},false);


如果您对jquery感到满意,


  $(document).ready(function(){
// your code
});

$(document).ready()触发DOMContentLoaded,但是这个事件并没有在浏览器中一致地被解雇。这就是为什么jQuery最有可能实现一些繁重的解决方法来支持所有的浏览器。这将使得使用简单的Javascript精确地模拟行为变得非常困难(当然不是不可能)。作为Jeffrey Sweeney和J Torres的建议,我认为它最好有一个 setTimeout 函数,然后触发如下的函数:

  setTimeout(function(){
// your code here
},3000);


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.

解决方案

this may work for you :

document.addEventListener('DOMContentLoaded', function() {
   // your code here
}, false);

or if your comfort with jquery,

$(document).ready(function(){
// your code
});

$(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).

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);

这篇关于在完成页面加载后调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆