无法将脚本加载到iframe中

阿列克谢

测试页: https //jsfiddle.net/y25rk55w/

在此测试页上,您可以看到3<iframe>相互嵌入。每个标签中<iframe>包含一个标签。<script><head>

问题是:只有<script>在第一个<iframe>会被浏览器加载。其他两个<script>标签将出现在dom中,但浏览器甚至不会尝试加载它们。该问题不是特定于浏览器的,可以在chrome,firefox中还原。无法通过添加超时或在追加脚本之前等待来解决此问题。所有iframe都必须以编程方式生成内容,这一点似乎很重要;如果您用具有实际src链接的iframe替换该iframe,问题将消失。

问题是:实际上如何将脚本加载到iframe 2和3中?

完整的测试代码:

// It doesn't matter if the scripts exist or not
// Browser won't try to load them either way
var scripts = [
    '//testdomain.test/script1.js',
    '//testdomain.test/script2.js',
    '//testdomain.test/script3.js'
];

function createIFrame(win, onCreated) {
    var iframe = win.document.createElement('iframe');
    iframe.onload = function () {
        onCreated(iframe);
    };
    win.document.body.appendChild(iframe);
}

function loadScript(win, url) {
    var script = win.document.createElement('script');
    script.src = url;
    script.onload = function() {
        console.log("Script " + url + " is loaded.");
    };
    win.document.getElementsByTagName('head')[0].appendChild(script);
}

createIFrame(window, function(iframe1) {
    loadScript(iframe1.contentWindow, scripts[0]);
    createIFrame(iframe1.contentWindow, function (iframe2) {
        loadScript(iframe2.contentWindow, scripts[1]);
        createIFrame(iframe2.contentWindow, function (iframe3) {
            loadScript(iframe3.contentWindow, scripts[2]);
        });
    });
});
阿列克谢

在问题中,您可以看到我正在忽略该协议:

/* This is valid to omit the http:/https: protocol.
   In that case, browser should automatically append 
   protocol used by the parent page */
var scripts = [
    '//testdomain.test/script1.js',
    '//testdomain.test/script2.js',
    '//testdomain.test/script3.js'
];

问题是,以编程方式创建的iframe具有协议about:(或javascript:,取决于您如何创建它们)。我仍然无法解释为什么第一个脚本正在加载,或者为什么其他两个脚本根本没有显示在“网络”选项卡中,但是我想这不是很重要。

解决方案:https://使用类似以下代码的方式显式使用或以编程方式附加协议:

function appendSchema(win, url) {
    if (url.startsWith('//')) {
        var protocol = 'https:';
        try {
            var wPrev = undefined;
            var wCur = win;
            while (wPrev != wCur) {
                console.log(wCur.location.protocol);
                if (wCur.location.protocol.startsWith("http")) {
                    protocol = wCur.location.protocol;
                    break;
                }
                wPrev = wCur;
                wCur = wCur.parent;
            }
        } catch (e) {
            /* We cannot get protocol of a cross-site iframe.
             * So in case we are inside cross-site iframe, and
             * there are no http/https iframes before it,
             * we will just use https: */
        }
        return protocol + url;
    }
    return url;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是否可以阻止将远程脚本加载到iframe中?

来自分类Dev

将脚本加载到iframe中,然后再加载到node-webkit中

来自分类Dev

无法使用angularJS将网址加载到iframe中

来自分类Dev

无法将脚本文件加载到另一个脚本文件中

来自分类Dev

无法通过AngularJS控制器将网址加载到iframe中

来自分类Dev

为什么使用Jquery无法将YouTube视频加载到iframe中?

来自分类Dev

无法将js加载到MongoDB中

来自分类Dev

如何使用php代理将网站加载到iframe中?

来自分类Dev

Firefox插件SDK:将插件文件加载到iframe中

来自分类Dev

使用Flask将网址加载到iFrame中

来自分类Dev

如何将 iframe 的内容加载到 Visual Basic 中?

来自分类Dev

jQuery将内容加载到firefox中的脚本标签中

来自分类Dev

将数组从Java脚本加载到网页中

来自分类Dev

如何将模块预加载到 python 脚本中?

来自分类Dev

无法将HTML中的AJAX加载到div中

来自分类Dev

无法将GCS中的CSV文件加载到bigquery中

来自分类Dev

使用javascript将页面加载到iframe

来自分类Dev

无法将示例文件加载到hadoop 2.2.0中

来自分类Dev

无法将简单的网格物体加载到OpenGL中

来自分类Dev

无法将大图像加载到Web View Android中

来自分类Dev

无法将数据加载到javafx表格中

来自分类Dev

无法将数据加载到配置单元表中

来自分类Dev

QDialog:无法从用户输入将数据加载到QTableWidget中

来自分类Dev

无法将数据加载到Pig中的Hortonworks沙箱

来自分类Dev

无法将ArrayList数据加载到Android中的微调器

来自分类Dev

无法将简单的网格物体加载到OpenGL中

来自分类Dev

无法将JavaScript文件加载到Django应用中

来自分类Dev

ember JSONAPIAdapter无法将数据正确加载到存储中

来自分类Dev

无法将更新的列表重新加载到datagridview中

Related 相关文章

  1. 1

    是否可以阻止将远程脚本加载到iframe中?

  2. 2

    将脚本加载到iframe中,然后再加载到node-webkit中

  3. 3

    无法使用angularJS将网址加载到iframe中

  4. 4

    无法将脚本文件加载到另一个脚本文件中

  5. 5

    无法通过AngularJS控制器将网址加载到iframe中

  6. 6

    为什么使用Jquery无法将YouTube视频加载到iframe中?

  7. 7

    无法将js加载到MongoDB中

  8. 8

    如何使用php代理将网站加载到iframe中?

  9. 9

    Firefox插件SDK:将插件文件加载到iframe中

  10. 10

    使用Flask将网址加载到iFrame中

  11. 11

    如何将 iframe 的内容加载到 Visual Basic 中?

  12. 12

    jQuery将内容加载到firefox中的脚本标签中

  13. 13

    将数组从Java脚本加载到网页中

  14. 14

    如何将模块预加载到 python 脚本中?

  15. 15

    无法将HTML中的AJAX加载到div中

  16. 16

    无法将GCS中的CSV文件加载到bigquery中

  17. 17

    使用javascript将页面加载到iframe

  18. 18

    无法将示例文件加载到hadoop 2.2.0中

  19. 19

    无法将简单的网格物体加载到OpenGL中

  20. 20

    无法将大图像加载到Web View Android中

  21. 21

    无法将数据加载到javafx表格中

  22. 22

    无法将数据加载到配置单元表中

  23. 23

    QDialog:无法从用户输入将数据加载到QTableWidget中

  24. 24

    无法将数据加载到Pig中的Hortonworks沙箱

  25. 25

    无法将ArrayList数据加载到Android中的微调器

  26. 26

    无法将简单的网格物体加载到OpenGL中

  27. 27

    无法将JavaScript文件加载到Django应用中

  28. 28

    ember JSONAPIAdapter无法将数据正确加载到存储中

  29. 29

    无法将更新的列表重新加载到datagridview中

热门标签

归档