1

I have a page which contains 4 Iframe, and all the pages add the same javascript. When the page loaded, I found the same javascript loaded from server 5 times. And then I set all Iframes loads when the javascript is loaded in the parent page, but get the same results, WHY?

  • The Code of the parent page:

    <html>
    
    <head>
    </head>
    
    <body>
        <iframe></iframe>
        <iframe></iframe>
        <iframe></iframe>
        <iframe></iframe>
    </body>
    <script>
        var newScript = document.createElement("script");
        newScript.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js";
    
        var srcs = ["html1.html", "html2.html", "html3.html", "html4.html"];
        document.getElementsByTagName("HEAD").item(0).appendChild(newScript);
        newScript.onload = newScript.onreadystatechange = function () {
            console.log("jquery.min.js loaded");
            var iframes = document.getElementsByTagName("iframe");
            for (var i = 0; i < iframes.length; i++) {
                var id = i;
                iframes[id].src = srcs[id];
            }
        }
        console.log("init");
    </script>
    
    </html>
    
  • The code in all iframe page:

    <html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=8">
        <meta http-equiv="Expires" content="0">
        <meta http-equiv="Pragma" content="no-cache">
        <meta http-equiv="Cache-control" content="no-cache">
        <meta http-equiv="Cache" content="no-cache">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    
    <body>
        html1
    </body>
    
    </html>
    
  • Image: Network of DevTool:

    enter image description here

    enter image description here

yidane
  • 11
  • 4

1 Answers1

0

An iframe has different context. Its like a separate browser tab or window. So its quite normal I think.

factor5
  • 321
  • 2
  • 12
  • But why An frame could not load Javascript which has been loaded? – yidane Nov 30 '18 at 07:57
  • I'm not sure what do you mean by "iframe to load the javascript which has been loaded", but if you mean that you want to load the script once in the parent window and use it in the iframes, then the answer is you cant and this is due to security constraints in the browser I think. – factor5 Nov 30 '18 at 09:06
  • Yeap, I meant that. Where can I get some document about what you said ' due to security'? – yidane Nov 30 '18 at 09:50
  • https://html.spec.whatwg.org/multipage/browsers.html#security-nav probably this might help – factor5 Nov 30 '18 at 09:57
  • Thinks,@factor5 – yidane Dec 13 '18 at 00:45