jQuery:在子窗口上接收文档 ready()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: receive document ready() on child window
提问by mrmclovin
I'm trying to get notified when the child window I'm opening has its document loaded and ready. This doesn't seem to work:
我正在尝试在我打开的子窗口加载并准备好其文档时收到通知。这似乎不起作用:
win = window.open(href, 'test', 'width=300, height=400');
win.focus();
$(win.document).ready(function() {
// Ok, the function will reach here but if I try to manipulate the
// DOM it doesn't work unless I use breakpoints
$(this).contents().find("...").doStuff(); // nothing happens
});
What do I have to do?
我需要做什么?
采纳答案by polarblau
Have you tried this? —
你试过这个吗?—
$(win.document).ready(function() {
$(win.document).contents().find("...").doStuff();
});
This questiondiscusses something very similar. Duplicate?
这个问题讨论了非常相似的事情。复制?
回答by Gunni
I had a similar problem and for me the .load event on the window did work, the .ready did not. So you may try:
我有一个类似的问题,对我来说,窗口上的 .load 事件确实有效,而 .ready 没有。所以你可以试试:
win = window.open(href, 'test', 'width=300, height=400');
$(win).load(function() {
$(this).contents().find("...").doStuff();
});
回答by Radek Benkel
Use window.opener
in script on site, which you are loading and execute function defined in global (!) at first page.
window.opener
在现场脚本中使用,您正在加载并执行第一页全局 (!) 中定义的函数。
Main page:
主页:
<html>
<head>
<script type="text/javascript">
window.notify = function () {
alert('runned from opened window');
};
window.onload = function() {
document.getElementById('button').addEventListener('click', function() {
window.open('test.html');
}, false);
};
</script>
</head>
<body>
<button id="button">Open window</button>
</body>
Opened page:
打开的页面:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
window.opener.notify()
};
</script>
</head>
<body>
Popup site
</body>
</html>
回答by vkGunasekaran
Simply add this code in iframe,
只需在 iframe 中添加此代码,
$(document,parent.document).ready(function(){
alert('Done');
});