一个页面中执行两个window.onload函数

16 篇文章 0 订阅

1.问题描述:

当一个HTML页面中加载了两个window.οnlοad=function() {};函数时,页面只会执行第二个window.onload里面的内容

如下,结果显示2:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2. <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
  5. <title>test</title>   
  6. <script language="javascript">   
  7. window.onload = function() {  
  8.     alert('1');  
  9. };  
  10.   
  11. </script>   
  12. </head>   
  13. <body>  
  14.     <h1>表格</h1>  
  15.       
  16.     <script type="text/javascript">  
  17.         window.onload = function() {  
  18.             alert('2');  
  19.         };  
  20.     </script>  
  21. </body>   
  22. </html>    


2.解决方法如下,可以显示1,2:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2. <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
  5. <title>test</title>   
  6. <script language="javascript">   
  7. window.onload = function() {  
  8.     alert('1');  
  9. };  
  10.   
  11.   
  12.   
  13.   
  14. </script>   
  15. </head>   
  16. <body>  
  17.     <h1>表格</h1>  
  18.       
  19.     <script type="text/javascript">  
  20.         var saved;  
  21.         if (typeof window.onload == 'function') {  
  22.             saved = window.onload;  
  23.         }  
  24.         window.onload = function() {  
  25.             if (saved) saved();  
  26.             alert('2');  
  27.         };  
  28.     </script>  
  29. </body>   
  30. </html>    

3.优化成函数:

[javascript]  view plain copy
  1. function addEvent(obj,evt,fn) {                                           
  2.     var saved;  
  3.     if (typeof obj["on"+evt] == "function") {  
  4.         saved = obj["on"+evt];  
  5.     }  
  6.     obj["on"+evt] = function () {  
  7.         if (saved) saved();       
  8.         fn();                 
  9.     }                     
  10. }  


 

如下,跟2一样:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  2. <html xmlns="http://www.w3.org/1999/xhtml">   
  3. <head>   
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
  5. <title>test</title>   
  6. <script language="javascript">   
  7. function addEvent(obj,evt,fn) {                                           
  8.     var saved;  
  9.     if (typeof obj["on"+evt] == "function") {  
  10.         saved = obj["on"+evt];  
  11.     }  
  12.     obj["on"+evt] = function () {  
  13.         if (saved) saved();       
  14.         fn();                 
  15.     }                     
  16. }  
  17. addEvent(window,'load',firstAll);  
  18. function firstAll() {  
  19.     alert('1');  
  20. }  
  21. </script>   
  22. </head>   
  23. <body>  
  24.     <h1>表格</h1>  
  25.       
  26.     <script type="text/javascript">  
  27.         addEvent(window,'load',nextAll);  
  28.         function nextAll() {  
  29.             alert('2');  
  30.         }  
  31.     </script>  
  32. </body>   
  33. </html>    
  34. http://blog.csdn.net/van416521/article/details/6953556#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值