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(); 這一句 可以將它做成循環一定次數。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章