Document. Madness.

I probably spent a month studying the Internet on this issue and have not yet found the answer to this question. My code does the following (all Javascript).

Let's say I have test.html on mydomain.com

  • First of all, in the chapter section, I was given document.domain = 'mydomain.com';

  • Then dynamically create an iframe, set src to "subdomain.mydomain.com/test2.html"

  • Add iframe to DOM

  • subdomain.mydomain.com/test2.html: the very first in the main section: document.domain = 'mydomain.com';

  • test2.html has an on_dom_ready event that tries to communicate with the parent through window.parent

It works in all browsers. even in IE6! The only problem: when I refresh the page in IE, I get an access denied error.

The only way to get rid of this error is to wait 12 seconds before calling window.parent. Even 5 seconds did not help, I have to wait 12 seconds. It makes no sense to me.

Does anyone have any experience?

+5
source share
2 answers

This is because the onload event in the parent frame is not yet fired, so the DOM is not completely built. Here's a kludge that will check the div at intervals until it is present, without bloating:

var minmax_SCANDELAY= 500;
var minmax_scanner;

function minmax_scan() {
    if (!window.parent.document.getElementById('content')) return;
    window.clearInterval(minmax_scanner);

    //replace following function call with your own.
    doYourMagicHere();
}

minmax_scan();
minmax_scanner= window.setInterval(minmax_scan, minmax_SCANDELAY);
+2
source

Furtive Answer really helped me! He was 100% right that the onload event is indeed a problem. I looked deeper into my code, and I found that before creating the child iframe, I was lazy to load some scripts using the technique below:

lazy_load: function(url,on_finish_function)
{
    var head = document.getElementsByTagName("head")[0];
    var script = document.createElement("script");
    script.src = url;
    var done = false;

    var This = this;
    // Attach handlers for all browsers
    script.onload = script.onreadystatechange = function()
    {
        if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete"))
        {
            done = true;
            on_finish_function.call(This,null);
            head.removeChild( script );
        }
    };
    head.appendChild(script);
},

HEAD . iframe on_finish lazy_load.

, IE 12 , , :) ? IE sooo lazy_load dom?

0

All Articles