我有一个案例,后端将html发送到包含iframe的jsp页面。 iframe src是通过ajax调用设置的,如下所示:
$(document).ready(function() {
$.ajax({
type: "get",
url: "http://someurl/",
success: function(msg){
console.log("Success" + msg);
$('#frameId').attr('src','data:text/html,'+ msg);
}
});
});
但是,设置的html包含脚本标记,其中包含window.onload函数,如下所示:
<script>
window.onload = function () {
//some processing done here
}
据我所知,这个onload不会被调用,因为iframe src是由ajax调用设置的,而iframe已经加载了。如何调用此函数?
答案 0 :(得分:1)
在设置HTML后,您似乎可以手动触发iframe中的onload
:https://stackoverflow.com/a/26339638/176615
答案 1 :(得分:0)
ActiveAdmin.register Email do
permit_params :name, :title, :html_content
form do |f|
f.inputs 'Blog Post' do
f.input :name
f.input :title
f.input :html_content, input_html: { class: 'summernote' }
end
f.actions
end
end
事件由框架文档触发,而不是包含它的iframe。因此,框架页面上的事件将自动触发,而您根本不做任何事情;框架页面不知道或不关心它是如何加载的。
以下是我用来确认的内容:
的index.html:
onload
的test.html:
<body>
<iframe id="x"></iframe>
<script>
// you can wrap this in a setTimeout too, just to confirm the ajax call doesn't interfere; same result
document.getElementById('x').setAttribute('src', 'test.html');
</script>
</body>