2011-12-02 32 views
3

HTML父窗口和iFrame內容相互瞭解,並且當它們位於同一個Web服務器上時可以自由通信。當他們被保存到DVD時,當iFrame嘗試以本地文件的形式聯繫top時,Chrome會拋出「不安全的JavaScript嘗試訪問帶有URL的幀」。如何在「不安全的JavaScript嘗試訪問帶有URL的框架」之前測試瀏覽器的權限(Chrome本地框架)?

下面的catch捕獲權限錯誤,但錯誤仍然由瀏覽器註冊並且對用戶可見。

如果在嘗試訪問之前允許訪問以排除不安全的JavaScript錯誤,是否有可能首先進行測試?

  // Called from script in an iframe 
      function findSiblingIFrame(sibId) { 
       try { 
         var sibFrame = top.document.getElementById(sibId); 
         if (sibFrame != null) { 
          alert("found sibling iframe"); 
         } else { 
          alert("did not find sibling iframe"); 
         } 
        } 
        catch (err) { 
         alert("not allowed to find sibling iframe"); 
         // Would rather test if permission first to prevent 
         // browser from registering the error. 
        } 
      } 
+0

查看http://www.netomatix.com/development/webcaspermissions.aspx。它可能會或可能不會幫助您作爲起點。 – MethodMan

+1

感謝您的鏈接,但檢查瀏覽器權限級別對此情況無效。我正在尋找一種乾淨的方式來確定父級是否與孩子在同一個域中,而不必通過parent.location.xyz調用產生跨站點腳本錯誤。就好像不得不在爐子上放一隻手來測試爐子是否很熱。這只是不舒服的解釋,FireFox放在瀏覽器頁面上的大紅色ERROR ON PAGE圖標意味着代碼正常工作。 – spiraleddy

回答

0

我最終使用HTML5消息傳遞來傳遞潛在的沙盒請求上下iframe層次結構。

例如,嵌套iframe層次結構中的每個html頁面都可以訪問以下javascript。如果捕獲的HTML5消息請求無法在本地執行,則消息將傳遞給父級。父母也可以將消息傳遞給iframe。這僅適用於所有頁面都可以訪問相同的JavaScript文件。

// function to handle message request 
function messageHandler(argJSON) { 
    // A collection of available functions for inbound messages 
    var msgFunctionMap = new Object(); 
    msgFunctionMap.removeBorder = removeBorder; 
    msgFunctionMap.restoreBorder = restoreBorder; 
    // ...more 
    // try execute request 
    try { 
     var jsonObj = JSON.parse(argJSON.data); 
     msgFunctionMap[jsonObj.request](jsonObj.args); 
    } 
    catch (err) { 
     alert(" Request not supported: " + argJSON.data); 
    } 
}; 
// example function to remove object id x's border if it exists in "this" window, else pass request up 
var removeBorder = function (jsonMsg, argObj) { 
    var xiFrame = document.getElementById("x"); 
    if (xiFrame != null) { 
     xiOrigWidth = xiFrame.style.borderWidth; 
     xiFrame.style.borderWidth = '0px'; 
    } 
    // Otherwise, pass message up else if (window.parent && window.parent.postMessage) { 
     window.parent.postMessage(jsonMsg.data, "*"); 
    } 
}; 
//... more 
// pass predefined message request from child to parent 
function messageUpHandler(message) { 
    if (window.parent && window.parent.postMessage) { 
     window.parent.postMessage(message.data, "*"); 
    } 
}; 
// Listener for child messages 
if (window.addEventListener) { 
    window.addEventListener("message", messageUpHandler, true); 
} 
0

只是檢查window.location.protocol,然後你可以有不同的行爲羯羊它是一個Web服務器上運行(HTTP:)或局部(文件:)。

你應該知道,雖然不同的瀏覽器對這些東西有不同的權限,所以你也應該檢查用戶的瀏覽器。

+0

這只是具有(文件:)問題的Chrome,但我擔心(http:// :)也可能開始將iframe限制爲父級通信。看到我下面發佈的結果 – spiraleddy

相關問題