Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

javascript - 完成页面加载后执行功能(execute function after complete page load)

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.(我想要的是调用页面完全加载的函数。)   ask by Neeraj Kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this may work for you :(这可能为您工作:)

document.addEventListener('DOMContentLoaded', function() { // your code here }, false); or if your comfort with jquery,(或者,如果您对jquery感到满意,) $(document).ready(function(){ // your code }); $(document).ready() fires on DOMContentLoaded, but this event is not being fired consistently among browsers.($(document).ready()在DOMContentLoaded上触发,但是此事件在浏览器之间未始终触发。) This is why jQuery will most probably implement some heavy workarounds to support all the browsers.(这就是为什么jQuery最有可能实施一些繁重的解决方法来支持所有浏览器的原因。) And this will make it very difficult to "exactly" simulate the behavior using plain Javascript (but not impossible of course).(这将使使用纯Javascript很难“精确地”模拟行为(当然并非不可能)。) as Jeffrey Sweeney and J Torres suggested, i think its better to have a setTimeout function, before firing the function like below :(正如Jeffrey Sweeney和J Torres所建议的那样,我认为在触发如下函数之前最好具有setTimeout函数:) setTimeout(function(){ //your code here }, 3000);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...