這期內容當中小編將會給大家?guī)碛嘘P如何跨域修改iframe頁面內容,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)擁有十多年的建站服務經(jīng)驗,在此期間,我們發(fā)現(xiàn)較多的客戶在挑選建站服務商前都非常的猶豫。主要問題集中:在無法預知自己的網(wǎng)站呈現(xiàn)的效果是什么樣的?也無法判斷選擇的服務商設計出來的網(wǎng)頁效果自己是否會滿意?創(chuàng)新互聯(lián)業(yè)務涵蓋了互聯(lián)網(wǎng)平臺網(wǎng)站建設、移動平臺網(wǎng)站制作、網(wǎng)絡推廣、按需開發(fā)網(wǎng)站等服務。創(chuàng)新互聯(lián)網(wǎng)站開發(fā)公司本著不拘一格的網(wǎng)站視覺設計和網(wǎng)站開發(fā)技術相結合,為企業(yè)做網(wǎng)站提供成熟的網(wǎng)站設計方案。
原理
主站點內嵌代理頁面, 并向代理頁傳遞數(shù)據(jù), 代理頁根據(jù)主站點的數(shù)據(jù)對目標頁的DOM進行操作.由于代理頁與目標頁同域, 所以代理頁可以獲取并操作目標頁的document對象.
前提條件
需要將proxy.html放到與內嵌的iframe頁同域的服務下, 并且可以被訪問到.
使用
支持2種調用方式: 使用 postMessage 和 URL params.
postMessage
該方法需要使用 JSON.stringify 將對象轉為字符串.
// React function IframeProxy(props) { handleLoad = (e) => { e.target.contentWindow.postMessage(JSON.stringify({ iframe: `<iframe name="target" title="target" className="target" src="http://www.targetdomain.com/target.html" frameBorder="0" scrolling="no" style="width: 100%;height:100%"></iframe>`, includeStyle: ` body { background-color: yellow; } header { display: none; } footer { display: none; } `, includeScript: ` window.addEventListener('load', function() { alert(document.querySelector('body').innerHTML); }); `, importStyle: `http://www.mydomain.com/assets/css/import.css`, importScript: `http://www.mydomain.com/assets/js/import.js` }), 'https://www.target.com'); } return <iframe name="proxy" title="proxy" className="proxy" width="100%" height="100%" onLoad={handleLoad} src={`http://www.targetdomain.com/proxy.html?origin=${window.location.protocol}//${window.location.host}`} frameBorder="0" scrolling="no"></iframe>; }
URL params
該方法需要將傳遞的內容用 encodeURIComponent 編碼.
// React function IframeProxy(props) { var params = 'iframe=' + encodeURIComponent(` <iframe name="target" title="target" className="target" src="http://www.targetdomain.com/target.html" frameBorder="0" scrolling="no" style="width: 100%;height:100%"></iframe> `); params += '&includeStyle=' + encodeURIComponent(` body { background-color: red; } header { display: none; } footer { display: none; } `); params += '&includeScript=' + encodeURIComponent(` window.addEventListener('load', function(event) { alert(document.querySelector('body').innerHTML); }); `); params += '&importStyle=' + encodeURIComponent(` http://www.mydomain.com/assets/css/import.css `); params += '&importScript=' + encodeURIComponent(` http://www.mydomain.com/assets/js/import.js `); return <iframe name="proxy" title="proxy" className="proxy" width="100%" height="100%" src={`http://www.targetdomain.com/proxy.html?${params}`} frameBorder="0" scrolling="no"></iframe>; }
API
<iframe src="http://www.targetdomain.com/proxy.html?params"></iframe>; params: { origin: 當前站點的域名, 使用postMessage方式時必填, proxy用來校驗發(fā)出消息的源域名. iframe: 需要內嵌的iframe標簽字符串, includeStyle: 希望添加到iframe頁的css內容, includeScript: 希望添加到iframe頁的js內容, importStyle: 希望引入到iframe頁的css資源鏈接, 如果目標站點使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會被瀏覽器禁止. importScript: 希望引入到iframe頁的js資源鏈接, 如果目標站點使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會被瀏覽器禁止. }
注意: 處于安全問題, 默認禁用了 includeScript 和 importScript 功能, 如需啟用在proxy.html中將變量 ENABLED_JS_INCLUDE 設置為 true 即可.
上述就是小編為大家分享的如何跨域修改iframe頁面內容了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章題目:如何跨域修改iframe頁面內容
當前地址:http://aaarwkj.com/article32/gdsgsc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、動態(tài)網(wǎng)站、建站公司、App設計、云服務器、服務器托管
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)