对 iframe 内的内容进行 ajax 替换时触发 iframe onload 事件
我有一个在域 A 上托管的信用卡付款表单的视图。域 B 有一个单独的网站,其中包含指向域 A 上的信用卡表单的 iframe。
域 A 上的表单使用这样的 ajax 表单:
<% using (Ajax.BeginForm("CreditCard", "Framed", new { id = Model.SID },
new AjaxOptions()
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "credit-card-wrapper",
LoadingElementId = "loading-pane"
},
new { id = "new-creditcard-form" }))
{ %>
然后在域 b 上,我正在使用自定义 jquery 插件通过允许外部事件更改 iframe 的 src 来管理我的 iframe。用法如下所示,这
$('#myIframe').frameplugin('creditcard');
会导致 iframe 将其 src 更改为信用卡页面。我还向 iframe 附加了一个 onload,如下所示:
$(this).bind('load', function () {
var src = $(this).attr('src').toLowerCase();
//do stuff based on url
});
我想做的是,当提交信用卡表单并完成替换时,我希望 iframe 内的文档触发 iframe 的 onload 事件,以便域 b 知道有事发生了。
另一种解决方案是以某种方式将自定义事件绑定到 iframe 并在框架文档中触发它们。我不确定这是否有效。有什么想法吗?
编辑:
我尝试使用从 iframe 到父级的事件触发器,但出现错误。
不安全的 JavaScript 尝试访问 带有 URL 的框架 http://localhost:59905/Home/Framed 来自带有 URL 的框架 http://localhost:27412/Framed/Index/。 域、协议和端口必须 匹配。
这是我的事件绑定
$(document).bind('success:myframe', function (event, referenceId) {
//do something
});
,这是来自框架页面的 javascript 调用。这是从 ajax.beginform 中的 OnSuccess 调用的
function paymentReceived() {
var referenceNumber = $('#referenceNumber').text();
$(top.document).trigger('success:myframe', referenceNumber);
}
I have a view with a credit card payment form hosted on Domain A. Domain B has a seperate website with an iframe to the credit card form on domain A.
The form on domain A uses an ajax form like this:
<% using (Ajax.BeginForm("CreditCard", "Framed", new { id = Model.SID },
new AjaxOptions()
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "credit-card-wrapper",
LoadingElementId = "loading-pane"
},
new { id = "new-creditcard-form" }))
{ %>
Then on domain b, I am using a custom jquery plugin to manage my iframe by allowing external events to change the src of the iframe. The usage looks like this
$('#myIframe').frameplugin('creditcard');
Which causes the iframe to change it's src to the credit card page. I also have attached an onload to the iframe like this:
$(this).bind('load', function () {
var src = $(this).attr('src').toLowerCase();
//do stuff based on url
});
What I want to do is when the credit card form is submitted and the replacement is done, I want the document within the iframe to trigger the iframe's onload event, so that domain b knows something happened.
Another solution would be to somehow bind custom events to the iframe and trigger them within the framed document. I'm not sure how/if that would even work. Any ideas?
EDIT:
I am attempting to use an event trigger from the iframe up to the parent and I get an error.
Unsafe JavaScript attempt to access
frame with URL
http://localhost:59905/Home/Framed
from frame with URL
http://localhost:27412/Framed/Index/.
Domains, protocols and ports must
match.
Here's my event binding
$(document).bind('success:myframe', function (event, referenceId) {
//do something
});
and here's the javascript call from the framed page. This is called from OnSuccess in the ajax.beginform
function paymentReceived() {
var referenceNumber = $('#referenceNumber').text();
$(top.document).trigger('success:myframe', referenceNumber);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您只想将事件从 iFrame 传递到父窗口。如果我错了,请纠正我。
从你的 iFrame 中,你不能这样做吗?
编辑:
让我再试一次。
这对我有用:
I think you just want to pass events from iFrame to the parent window. Please correct me if I'm wrong.
From your iFrame, can't you just do this?
Edit:
Let me try again.
This works for me: