iframe跨域訪問父架構js方法

來源:互聯網
上載者:User

標籤:exe   add   開啟   call   跨域問題   cut   rand   jquer   abs   

iframe跨域訪問父架構js方法

條件

1.不在同一個主域下

2.iframe調用父架構方法

 

環境說明

父架構有開啟TAB頁方法,但是子頁面嵌入的是其他系統頁面,同樣需要開啟TAB頁。

 

方法描述

原理上就是通過子頁面嵌入父架構頁面來使用調用方法

父架構橋連頁面

 1 @{ 2     ViewBag.Title = "跨域橋連"; 3     Layout = "~/Views/Shared/_LayoutCenter.cshtml"; 4 } 5  6 @section Scripts{ 7 <script> 8     parent.parent.DomainAddtabs("@ViewBag.Url", "@ViewBag.Name"); // execute main function 9 </script>10 }

子系統js方法

 1 /* 2  跨域問題 3 */ 4 ; (function ($) { 5     $.extend({ 6         sendDomain: function (opt, callback) { 7             var defaults = { 8                 wmsUrl: ‘#‘, 9                 icon: ‘icon-house_in‘, //表徵圖10                 title:‘title‘,11                 sapUrl: ‘#‘12             };13             var opts = $.extend(defaults, opt);14 15             try {16                 var parent = window.parent;17                 var url = opts.wmsUrl;18                 var icon = "icon " + opts.icon;19                 window.parent.addTab(opts.title, url, icon);20             } catch (e) {21                 var url = opts.wmsUrl;22                 $.execMain(url, opts.title, opts.sapUrl);23             }24             if (callback) {25                 callback();26             }27         },28         // exec main function29         execMain: function (url, name, mainUrl) {30             if (typeof (exec_obj) == ‘undefined‘) {31                 exec_obj = document.createElement(‘iframe‘);32                 exec_obj.name = ‘tmp_frame‘;33                 exec_obj.src = mainUrl + ‘?url=‘ + url + ‘&name=‘ + name;34                 exec_obj.style.display = ‘none‘;35                 document.body.appendChild(exec_obj);36             } else {37                 exec_obj.src = mainUrl + ‘?url=‘ + url + ‘&name=‘ + name + ‘&‘ + Math.random();38             }39         }40     });41 })(jQuery);

 

完成。

 

iframe跨域訪問父架構js方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.