页面加载完成后执行函数

编程入门 行业动态 更新时间:2024-10-04 07:27:16
本文介绍了页面加载完成后执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用以下代码在页面加载后执行一些语句.

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

更多推荐

页面加载完成后执行函数

本文发布于:2023-11-26 23:20:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   加载   页面   完成后

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!