使用
- window.onload = firstFunction;
函数,在网页加载完毕时只能触发一个onload事件
当出现如下情形时,页面加载时只会执行secondFunction函数:
- window.onload = firstFunction;
- window.onload = secondFunction;
解决办法①:
- window.onload = function(){
- firstFunction;
- secondFunction;
- }
解决办法②:
- function addLoadEvent(func){
- var oldonload = window.onload;
- if (typeof window.onload != "function"){
- window.onload = func;
- }
- else{
- window.onload = function(){
- oldonload();
- func();
- }
- }
- }
然后用如下方式进行添加函数:
- addLoadEvent(firstFunction);
- addLoadEvent(secondFunction);