window.onready = function()在IE中失败

时间:2010-01-05 17:01:03

标签: javascript javascript-events

我有一个严格基于浏览器的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");
  }        
}

2 个答案:

答案 0 :(得分:1)

最好的猜测是,在您的处理程序被分配时,ready事件已在目标窗口中触发 - 换句话说,您遇到了竞争条件。

我使用的解决方案是在content.html中使用window.opener在内联Javascript或ready处理程序中执行对父窗口的回调 content.html你的用例是否可行?

答案 1 :(得分:1)

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

不能保证这会起作用,但也许值得一试?