jQuery: receive document ready() on child window

↘锁芯ラ 提交于 2020-01-03 07:09:11

问题


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?


回答1:


Have you tried this? —

$(win.document).ready(function() {
    $(win.document).contents().find("...").doStuff();
});

This question discusses something very similar. Duplicate?




回答2:


I had a similar problem and for me the .load event on the window did work, the .ready did not. So you may try:

win = window.open(href, 'test', 'width=300, height=400');
$(win).load(function() {
    $(this).contents().find("...").doStuff(); 
});



回答3:


Use window.opener in script on site, which you are loading and execute function defined in global (!) at first page.

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>



回答4:


Simply add this code in iframe,

$(document,parent.document).ready(function(){
alert('Done');
});  


来源:https://stackoverflow.com/questions/4842432/jquery-receive-document-ready-on-child-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!