javascript window.onready =函数()在IE中失败

yi0zb3m4  于 2023-01-01  发布在  Java
关注(0)|答案(2)|浏览(154)

我有一个严格基于浏览器的JavaScript和xlst应用程序,它通常在本地机器上使用,但有时通过Apache访问。
主页面是content.html,它的内容可以动态改变,在一个例子中,一个页面中的href打开一个新窗口,最后执行下面的代码,设置一个onready函数,目的是构建新窗口的内容。
这在几乎所有情况下都有效。它在Firefox的所有情况下都有效。甚至在IE本地运行时(eidogg. file:///C:/Program%20Files/Apache%20Software%20Foundation/Apache2.2/htdocs/hlic/index.html),它在所有情况下都有效。
我的问题是,当通过Apache在IE中访问时,我只得到一个空白窗口。它的行为就像onready函数从未被激发过一样。然而,如果我添加一个alert("fire"),警报确实会显示,之后,我的窗口内容确实会显示。那么为什么它只对警报起作用呢?我还能做些什么来让内容显示呢?任何帮助都将不胜感激。
下面是代码:

/*
 *  PresentStreamInNewBrowser
 *
 *      Creates a new browser window and fills with
 *      provided content stream. Also sizes the
 *      window to either default dimensions or to any       
 *      supplied dimensions.
 *
 *      A close and a print button are added to the bottom 
 *      of the content stream.
 *
 *      Inputs:
 *          pageStream - content stream to display
 *              in new window.
 *          height, width, top, left - dimensions
 *              for the newly opened window. (optional)
 * 
 */ 
function Presenter_PresentStreamInNewBrowser( pageStream, height, width, top, left )
{
    // set the features
    var features  = "height=" + height;
        features += ",width=" + width;
        features += ",top=" + top;
        features += ",left=" + left;
        features += ",scrollbars=yes";
        features += ",resizable=yes";

    // open the window
    var presenter = this;
    var newWindow = window.open(
        app.NormalizeURI("deview/frames/content.html"), '', features );

    // the rest of the code executes when the content window
    // calls its onready function, after it has loaded
    newWindow.onready = function() {

    

        var doc = newWindow.document;

        // create stylesheet links if applicable
        for(var i = 0; i < pageStream.stylesheet.length; i++) {
            var linkElement = doc.createElement("link");
            linkElement.rel = "stylesheet";
            linkElement.href = pageStream.stylesheet[i];
            doc.getElementsByTagName("head")[0].appendChild( linkElement );
        }

        // insert content
        doc.body.innerHTML = "";
        doc.body.appendChild( pageStream.content.importIntoDocument(doc) );
        doc.body.appendChild( doc.createElement("br") );

        // Handle generation of special pages.
        presenter.AddGeneratedContent( doc );

        // add a close button
        var selectionString =
        "displayStrings/promptStrings/promptString[@id='close_button_anchor']";
        var buttontext = app.displayStringsDOM.selectSingleNode(
        selectionString ).firstChild.nodeValue;
        var closeButtonElement = doc.createElement("button");
        closeButtonElement.id = "closebutton";
        closeButtonElement.className = "addonbutton";
        closeButtonElement.onclick = "javascript:window.close()";
        closeButtonElement.value = buttontext;
        doc.body.appendChild( closeButtonElement );

        // add a print button
        selectionString =
        "displayStrings/buttonLabelStrings/buttonLabelString[@id='print_button_anchor']";
        buttontext = app.displayStringsDOM.selectSingleNode(
        selectionString ).firstChild.nodeValue;
        var printButtonElement = doc.createElement("button");
        printButtonElement.id = "printbutton";
        printButtonElement.className = "addonbutton";
        printButtonElement.onclick = "javascript:window.print()";
        printButtonElement.value = buttontext;
        doc.body.appendChild( printButtonElement );

        // close up shop
        newWindow.document.body.appendChild(
        newWindow.document.createElement("br") );
        newWindow.document.title = '-';

        // add some language dependent text
        presenter.AddButtonLabel( doc );
        presenter.AddExamLabels( doc );

        //alert("fire");
  }        
}
5w9g7ksd

5w9g7ksd1#

您也许可以尝试检查文档readyState,类似于

var newWindowReadyFired = false;

newWindow.onready = function() {
  if (newWindowReadyFired) return; //just in case race condition got us in here twice   
  newWindowReadyFired = true;

  //....the rest of your event handler
}

function chkFunc() {
  if (newWindowReadyFired) return; //magic already done

  if (newWindow.document && newWindow.document.readyState == 4) {
    newWindow.onready();
  } else {
    setTimeout(chkFunc,"100"); //wait and try again
  }
}
chkFunc();

不能保证这将工作,但也许值得一试?

cbeh67ev

cbeh67ev2#

最好的猜测是,在分配处理程序时,ready事件已经在目标窗口中触发-换句话说,您遇到了争用条件。
我使用的解决方案是让content.html本身中的某个东西使用window.opener在内联Javascript中或在ready处理程序 * 中执行对父窗口的回调 * content.html中的ready处理程序 * 在您的用例中可行吗?

相关问题