首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在iFrame中使用javascript包含外部html

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

Stack Overflow用户
提问于 2018-09-29 13:37:30
回答 1查看 190关注 0票数 0

我正在尝试使用rest服务获取带有javascript的html (在这个javascript脚本标记内具有相对路径),并将其显示在我的angularjs应用程序的iFrame中。

基本上,我们与webfocus集成,当我们调用rest服务时,webfocus提供html内容。此html内容中包含script标记,并且此script标记具有相对路径。因此,当我尝试将html/javascript绑定到iFrame contentWindow.document.body时,我遇到了相对路径问题。

任何帮助都将不胜感激。

Angularjs指令如下。

代码语言:javascript
复制
.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: '='
        }
    };
}])

超文本标记语言代码是<preview content="reportHtml"></preview>

I used this link to write this code.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-30 04:11:45

当我在iframe中添加基本标签时,问题得到了解决,因为here也在watch中使用了这3行代码,而不是分配内部html。

代码语言:javascript
复制
 iframe.contentWindow.document.open();
                iframe.contentWindow.document.write($sce.trustAsHtml(scope.content));
                iframe.contentWindow.document.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52565458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档