iframe上的onload事件在IE和Firefox / GoogleChrome中的工作方式不同
onload event on iframe working differently in ie and firefox/googlechrome
这是我的 iframe 的 ASPX 页面
<asp:UpdatePanel ID="dynamicForm" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<iframe id="iTargetLoader" name="iTargetLoader" width="200" height="50" onload="onLoadFn()"></iframe>
</ContentTemplate>
</asp:UpdatePanel>
这是我在JavaScript中用于iframe的Onload事件的Onloadfn函数
function onLoadFn() {
//debugger;
document.getElementById("iTargetLoader").contentWindow.document.defaultView.frameElement.innerText;
var frame = document.getElementById("iTargetLoader");
var contentDoc = (frame.contentWindow || frame.contentDocument);
if (contentDoc != null && contentDoc.document.body != null) {
var serverResult = contentDoc.document.body.innerText;
var regSuccess = /success/i;
if (regSuccess.test(serverResult)) {
if (parseInt($('#ContentPlaceHolder1_hdn_qstnadd').val()) == 1) {
addQuestion();
$('#ContentPlaceHolder1_hdn_qstnadd').val(0);
}
//alert('success!');
//return "success!";
}
else {
if ($.trim(serverResult) == "") {
//alert("empty!");
}
else {
alert("failed!" + serverResult + "Please Contact Your Administrator!");
window.parent.window.open("", "_self");
window.parent.window.close();
}
//return "failure!";
}
}
else {
return "";
}
}
在这里,来自 servlet 的反馈被加载到 iframe 中,即"iTargetLoader",我正在尝试在加载后检查 iframe 的内容是否成功。它在IE中工作正常,但是当我在onload事件中使用火狐或谷歌浏览器时,iframe的加载没有完成,因此内部html显示为null。但是在IE中,加载首先完成,innerhtml显示反馈。我该如何解决它?
所以你的问题更多的是让它跨浏览器兼容:然后让我们把怪癖拿出来...
HTML 或 ASPX
使用引用this
,这样就不需要多次获取该对象。
<iframe id="iTargetLoader" name="iTargetLoader" width="200" height="50" onload="javascript: onLoadFn(this);"></iframe>
.JS
// a global function needs to be defined first before it can be used
// most likely not an issue in this example but
// similar to addQuestion you might want to change it to IIFE
// http://en.wikipedia.org/wiki/Immediately-invoked_function_expression
function onLoadFn(frame) {
// define your variables
var contentDoc,
serverResult,
regSuccess = /success/i,
qstnadd;
// test your parameter exists, in this case the iframe (this)
if (frame) {
// Note: Because of security reasons, the contents of a document can be accessed from another document only if the two documents are located in the same domain.
// Note: Internet Explorer 8 (and higher) supports the contentDocument property only if a !DOCTYPE is specified. For earlier versions of IE, use the contentWindow property.
contentDoc = frame.contentWindow || frame.contentDocument;
if (contentDoc) {
// testing the proper document object here
if (contentDoc.document){
contentDoc = contentDoc.document;
}
serverResult = contentDoc.body.innerText || "";
if (regSuccess.test(serverResult)) {
// request your object once
// .Net supports something like IDMode static which can help to determine the ID here.
qstnadd = $('#ContentPlaceHolder1_hdn_qstnadd');
// use a raddix when using parseInt
if (parseInt(qstnadd.val(), 10) === 1) {
addQuestion();
qstnadd.val(0);
}
} else {
// looks trivial to me
if ($.trim(serverResult) === "") {
alert("empty");
} else {
alert("failed!" + serverResult + "Please Contact Your Administrator!");
window.parent.window.open("", "_self");
window.parent.window.close();
}
}
}
}
}
即使这不能直接解决您的问题,我相信您也可以通过这种方式更好地查明问题。疯狂猜测,您的问题出在contentDoc.document
,因为您请求的是window.document.body
或document.body
,但不是window.document.document.body
尽管后者可能在IE中工作^^
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- ajaxToolkit PopupControlExtender不工作.过时的
- HTML标记在脚本标记中工作
- iframe上的onload事件在IE和Firefox / GoogleChrome中的工作方式不同