JS window.name跨域封装
JS window.name 跨域封装
1 function CrossDomainName(target, agent, callback, security) { 2 if (typeof target !== 'string' || typeof agent !== 'string' || typeof callback !== 'function') { 3 throw '参数错误'; 4 } 5 this.state = 0; 6 this.target = target;//目标路径 7 this.agent = agent;//代理文件 8 this.callback = callback || Function;//回调函数 9 this.security = security || true; 10 this.iframe = document.createElement('iframe'); 11 var self = this; 12 function onload() { 13 if (self.state === 1) { 14 var data = self.iframe.contentWindow.name; 15 self.callback.call(self.iframe, data); 16 if (self) { 17 self.iframe.contentWindow.document.close();//关闭文档流 18 self.iframe.contentWindow.close();//关闭Iframe 19 document.body.removeChild(self.iframe);//移除Iframe 20 } 21 } else if (self.state === 0) { 22 self.state = 1; 23 self.iframe.contentWindow.location = self.agent; 24 } 25 } 26 if (document.attachEvent) { 27 this.iframe.attachEvent('onload', onload); 28 } else if (document.addEventListener) { 29 this.iframe.addEventListener('load', onload, false); 30 } else { 31 this.iframe.onload = onload; 32 } 33 this.iframe.src = this.target; 34 document.body.appendChild(this.iframe); 35 } 36 //调用 37 new CrossDomainName('http://www.kingwell.com/kingwell.html', 'http://name.b.com/adfas.html', function (data) { 38 alert(data); 39 });