2014-10-16 75 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