简体   繁体   English

如何在iFrame中使用javascript包含外部html

[英]how to include an external html with javascript in iFrame

I am trying to get html with javascript (with relative paths inside this javascript script tag) using rest service and show it in the iFrame in my angularjs application. 我正在尝试使用rest服务使用javascript(带有此javascript脚本标记内的相对路径)获取html,并将其显示在我的angularjs应用程序的iFrame中。

basically we are integrating with webfocus and webfocus provides html contents when we call the rest service. 基本上,我们正在与webfocus集成,并且在调用rest服务时,webfocus提供html内容。 This html content has script tag in it and this script tag has relative paths. 该html内容中包含脚本标签,并且此脚本标签具有相对路径。 so when I try to bind that html/javascript to iFrame's contentWindow.document.body I am getting relative path issue. 因此,当我尝试将html / javascript绑定到iFrame的contentWindow.document.body时,出现相对路径问题。

Any help would be much appreciated. 任何帮助将非常感激。

Angularjs directive is as follows. Angularjs指令如下。

.directive("preview", ['$sce',function ($sce) {
    function link(scope, element) {
        var iframe = document.createElement('iframe');
        iframe.setAttribute("id", "ifWebFocusContent");
        iframe.setAttribute("name", "nameWebFocusContent");

        var element0 = element[0];
        element0.appendChild(iframe);
        var body = iframe.contentWindow.document.body;
        //var body = iframe.document.body;

        scope.$watch('content', function () {                
            body.innerHTML = $sce.trustAsHtml(scope.content);
        });
    }

    return {
        link: link,
        restrict: 'E',
        scope: {
            content: '='
        }
    };
}])

And HTML code is <preview content="reportHtml"></preview> HTML代码是<preview content="reportHtml"></preview>

I used this link to write this code. 我使用此链接编写此代码。

It got resolved when I add base tag in the iframe as here Also used these 3 lines inside watch instead of assigning inner html. 当我在iframe中添加基础标签,因为它得到了解决这里也用这3个行内的手表,而不是分配给内部HTML。

 iframe.contentWindow.document.open();
                iframe.contentWindow.document.write($sce.trustAsHtml(scope.content));
                iframe.contentWindow.document.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM