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

Javascript window.onready=函数()在IE中失败,javascript,javascript-events,Javascript,Javascript Events,我有一个严格基于浏览器的javascript&xlst应用程序。它通常在本地机器上使用,但有时通过Apache访问 主页是content.html,它可以动态更改其内容。在一种情况下,一个页面中的href打开一个新窗口,并最终执行以下代码,该代码设置了onready函数,目的是生成新窗口的内容 这几乎适用于所有情况。它适用于Firefox的所有情况。它甚至可以在本地运行的IE的所有情况下工作(例如。file:///C:/Program%20Files/Apache%20Software%20Fo

我有一个严格基于浏览器的javascript&xlst应用程序。它通常在本地机器上使用,但有时通过Apache访问

主页是content.html,它可以动态更改其内容。在一种情况下,一个页面中的href打开一个新窗口,并最终执行以下代码,该代码设置了onready函数,目的是生成新窗口的内容

这几乎适用于所有情况。它适用于Firefox的所有情况。它甚至可以在本地运行的IE的所有情况下工作(例如。file:///C:/Program%20Files/Apache%20Software%20Foundation/Apache2.2/htdocs/hlic/index.html).

我的问题是,当在IE中通过Apache访问时,我只会得到一个空白窗口。它的行为就像onready函数从未被激发一样。但是,如果我添加了一个警报(“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");
  }        
}
/*
*PresentStreamInNewBrowser
*
*创建一个新的浏览器窗口并填充
*提供的内容流。还可以调整
*窗口设置为默认尺寸或任意尺寸
*提供的尺寸。
*
*底部添加了“关闭”和“打印”按钮
*内容流的一部分。
*
*投入:
*pageStream—要显示的内容流
*在新窗口中。
*高度、宽度、顶部、左侧-尺寸
*对于新打开的窗口。(可选)
* 
*/ 
功能演示者\u演示流InNewBrowser(页面流、高度、宽度、顶部、左侧)
{
//设置功能
var features=“height=”高度;
特征+=”,宽度=”+宽度;
功能+=”,top=“+top;
特征+=”,left=“+left;
功能+=”,滚动条=是”;
功能+=”,可调整大小=是”;
//打开窗户
var presenter=这个;
var newWindow=window.open(
app.NormalizeURI(“deview/frames/content.html”)、“”、功能);
//其余代码在内容窗口打开时执行
//加载后调用其onready函数
newWindow.onready=函数(){
var doc=newWindow.document;
//创建样式表链接(如果适用)
对于(var i=0;i
最好的猜测是,在分配处理程序时,
ready
事件已经在目标窗口中触发-换句话说,您遇到了竞争条件


我使用的解决方案是让content.html本身使用
window执行对父窗口的回调。opener
可以是内联Javascript,也可以是content.html中的
ready
处理程序,这在您的用例中可行吗?

您可以尝试检查文档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();

无法保证这会奏效,但或许值得一试?

谢谢您的回复。这个问题似乎确实与种族状况有关。这非常有帮助。非常感谢。在我的例子中,我可以从同一个页面启动多个弹出窗口,所有这些都使用相同的代码(content.html)。所以我只是不检查onready函数中是否有newWindowReadyFired。实际上,这样做对您来说还是安全的——“javascript闭包”(作用域,允许内部方法访问外部方法的变量)应该意味着每次调用Presenter_PresentStreamInNewBrowser()时都会创建该变量的新实例方法,并在检查中引用正确的实例。我对此有90%的把握(这也意味着10%的不确定性,所以你还是想自己检查一下,也许可以在第二个窗口加载时放入一个alert()来显示var的值并检查它)