我有一个严格基于浏览器的 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
函数永远不会被触发。但是,如果我添加 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");
}
}
最佳答案
您也许可以尝试检查文档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();
不能保证这会起作用,但也许值得一试?
关于javascript - window.onready = function() 在 IE 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2007659/