JS 实现跨页事件响应

如题:在工作中遇到问题。主页面发出事件,该页面中嵌入的iframe的页面也要响应该事件操作。例如主页面登录后,该页面加载的iframe页也要根据登录情况作出不同的显示。

 

主页面

  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=utf-8" />
  5. <title>主页</title>
  6. <script src="base.js" language="javascript"></script>
  7. <script>
  8. function callsub(){
  9.     
  10.     CliBase.getInstance().dispatchEvent( new Event('testcall','success'));
  11. }
  12. function testhandler(e){
  13.     alert("来自主页面的:"+e.args)
  14. }
  15. CliBase.getInstance().addEventListener('testcall',testhandler)
  16. </script>
  17. </head>
  18. <body>
  19. 主页面
  20. <a href="javascript:callsub();">呼叫子页面</a>
  21. <iframe  id="aa" src="sub.html" height="500" width="100%" ></iframe>
  22. </body>
  23. </html>

子页面

  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=utf-8" />
  5. <title>subpage</title>
  6. <script src="base.js" language="javascript"></script>
  7. <script>
  8. window.onload = function(){
  9. if(window.parent!=null){
  10.     CliBase.getInstance().addEventListener('testcall',testhandler);
  11. }
  12. }
  13. function testhandler(e){
  14.     document.getElementById("output").innerHTML="来自子页面的"+e.args;
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. ifamre中的页面
  20. <div id="output" align="ouput"></div>
  21. </body>
  22. </html>

他们共同都需要加载的js文件

 

  1. // JavaScript Document
  2. function Event(type,args)
  3. {this.type = type;
  4. this.args = args;}
  5. /**
  6.  * @author andypan
  7.  * 操作基类
  8. */
  9. var CliBase = (function() {                 
  10.     var _eventList = new Array();
  11.     var uniqueInstance; // Private attribute that holds the single instance.
  12.     // Return the constructor.
  13.     return function() { 
  14.     // implements Publication
  15.     this.getEventList = function() {return _eventList;}
  16. }})();
  17. CliBase.prototype.addEventListener = function(EventType,handler){
  18.     this.getEventList().push([EventType,handler]);
  19. }
  20. CliBase.prototype.removeEventListener = function(EventType,handler){
  21.     for(var i=0;i<this.getEventList().length;i++){
  22.         if((EventType==this.getEventList()[i][0])&&(handler==this.getEventList()[i][1])){
  23.             this.getEventList().splice(i,1);
  24.         }
  25.     }
  26. }
  27. CliBase.prototype.dispatchEvent = function(Event){
  28.     for(var i=0;i<this.getEventList().length;i++){
  29.         if(Event.type==this.getEventList()[i][0]){
  30.             this.getEventList()[i][1](Event);
  31.         }
  32.     }
  33. }
  34. CliBase.getInstance = function(){
  35.     if(this.instance ==nullthis.instance = new CliBase();
  36.     if(window.parent!=window){
  37.         //子页面被实例化后,替换现有实例
  38.         this.instance = window.parent.CliBase.getInstance();
  39.     }
  40.     return this.instance;
  41. }

 

 

我觉得关键就是  this.instance = window.parent.CliBase.getInstance(); 这一句 可以将它做成循环一定次数。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值