2014-10-16 107 views

回答

0

我們可以在跨域環境中使用IFRAME。但我們必須使用阿賈克斯調用IFRAME配置。以下是代碼。它爲我工作。我搜查了很多地區。 Atlast我得到了這個工作代碼。

HTML

<iframe id="doc-frame" src="javascript:(function() {document.open();document.domain='localhost';document.close();})();" style="width:100%;height:100%;" seamless="seamless" scrolling="no" frameborder="0"></iframe> 

在IFRAME我們必須設置域作爲請求窗口文件的同一個域。否則它將無法工作。這裏我設置了localhost。

JAVASCRIPT

$.support.cors = true; 
      var frameSize = 0; 
      $("#doc-frame").css({ 'height': frameSize, 'width': frameSize, 'zoom': '100%' }); 
      $.ajax({ 
       url: fileNamePath,// URL of the request page 
       context: document.body, 
       success: function (response) { 
        target = $("#doc-frame")[0].contentDocument; 
        target.open(); 
        target.write(response); 
        target.close(); 
        var frameMargin = 10; 
        $("#doc-frame").css("height", parseFloat($('#doc-frame').contents().height() + frameMargin) + "px"); 
        $('#doc-frame').css("width", parseFloat($("#doc-frame").contents().width() + frameMargin) + "px"); 
       } 
      }); 

它將工作。請使用此代碼,並讓我知道評論,缺點和限制。我不認爲這可以滿足我們所有的跨域IFRAME的需求。但在某些情況下,我們可以使用它。

謝謝

+1

爲什麼重寫jQuery的CORS支持檢測?如果jQuery嘗試在不支持它的瀏覽器中使用CORS會有什麼好處呢? – Quentin 2014-10-16 10:37:07

+0

一些請求只在我使用CORS時完成...... – 2014-10-16 10:50:08

+0

覆蓋jQuery的CORS支持檢測不是「使用CORS」。 – Quentin 2014-10-16 10:51:18