1 //Coding by : bhwebxu tsui
2
3 //@param{String}id id of iframe
4 //@param{String}parent_id container of iframe
5 //@param{String}url url of iframe
6 //@param{Number}height default $(window).height();
7 //@param{Number}width default 100%
8 var Iframe = function(id, parent_id, url, height, width) {9
10 'use strict';11
12 if (!this instanceof Iframe) return newIframe(id, parent_id, url, height, width);13
14 this.id = id || 'newIframe';15 this.parent_id =parent_id;16 this.url =url;17 this.width = width ? width + 'px' : '100%';18 this.height = (height || $(window).height()) + 'px';19
20 //set iframe height
21 //@param{Number}height
22 this.setHeight = function(height) {23 $('#' + this.id).animate({ height: height + 'px'});24 };25
26 //get ifrme window obj
27 //@return{Object}
28 this.getWindow = function() {29
30 var doc, iframe = document.getElementById(this.id);31
32 if (iframe.contentWindow) { returniframe.contentWindow; }33 if (iframe.window) { returniframe.window; }34 if (!doc && iframe.contentDocument) { doc =iframe.contentDocument; }35 if (!doc && iframe.document) { doc =iframe.document; }36 if (doc && doc.defaultView) { returndoc.defaultView; }37 if (doc && doc.parentWindow) { returndoc.parentWindow; }38
39 returnundefined;40 };41
42 //create iframe
43 //@param{Function}callback excute this method when iframe load completed
44 this.create = function(callback) {45
46 var doc =document,47 iframeContainer = doc.getElementById(this.parent_id),48 iframeContainerNodes = 0,49 iframeNew = doc.createElement('iframe');50
51 iframeNew.setAttribute('id', this.id);52 iframeNew.setAttribute('src', this.url);53 iframeNew.setAttribute('border', 0);54 iframeNew.setAttribute('frameBorder', 0);55 iframeNew.setAttribute('marginWidth', 0);56 iframeNew.setAttribute('marginHeight', 0);57 iframeNew.setAttribute('scrolling', 'no');58 iframeNew.setAttribute('allowtransparency', 'no');59 iframeNew.style.width = this.width;60 iframeNew.style.height = this.height;61
62 if(iframeNew.attachEvent) {63 iframeNew.attachEvent('onload', function() {64 //console.log('ie');
65 if(callback) {66 callback();67 }68 });69 } else{70 iframeNew.onload = function() {71 //console.log('ff');
72 if(callback) {73 callback();74 }75 };76 }77
78 if(iframeContainer) {79
80 //clear container nodes
81 if(iframeContainer.hasChildNodes()) {82 iframeContainerNodes =iframeContainer.childNodes;83 for (var i = 0, l = iframeContainerNodes.length; i < l; i++) {84 iframeContainer.removeChild(iframeContainerNodes[i]);85 }86 }87
88 //append iframe to container
89 iframeContainer.appendChild(iframeNew);90
91 } else{92 alert('未找到iframe父节点');93 }94 };95 };96
97
98 //parent iframe code here
99 ;(function(Controller,doc){100
101 Controller.Const={102 url:'subiframe.html'
103 };104
105 Controller.View={106
107 initializeObj:function(){108
109
110 },111
112 IFrame:{113 create:function(){114
115 var iframe_id = 'iframeSub',116 iframe = newIframe(iframe_id117 , 'divContainer'
118 , Controller.Const.url119 , 300);120 iframe.create(function() {121
122 //api for subiframe
123 var iframeWin =iframe.getWindow();124 iframeWin.MainPageObj =Controller.PublicMethod;125
126 //access subiframe method,when subiframe load completed
127 iframeWin.SubPageObj.initialize();128 });129 }130
131 }132 };133
134
135 //public method for subiframe
136 Controller.PublicMethod={137
138 set:function(){139
140 //initialize parent iframe obj
141 Controller.View.initializeObj();142 }143 };144
145 //export
146 returnController.PublicMethod;147
148 })(MainPageObj||{},document);149
150
151 //subiframe code here
152 ;(function(Controller,doc){153
154 Controller.initialize=function(){155
156 //initialize subiframe
157 };158
159 //api for parent iframe
160 Controller.PublicMethod={161
162 initialize:function(){163
164 //initialize subiframe here
165 Controller.initialize();166 }167 };168
169 //access parent iframe
170 Controller.AccessParentPage={171
172 set:function() {173
174 //access parent iframe method
175 MainPageObj.set();176 }177 };178
179 //export
180 returnController.PublicMethod;181
182 })(SubPageObj||{},document);