简体   繁体   English

父窗口中的jQuery表达式无法从弹出窗口正常工作-怎么了?

[英]jquery expression in parent window not working properly from popup window - whats wrong?

i am refreshing parent window from popup window,then calling parent window jquery expressions not works. 我正在从弹出窗口刷新父窗口,然后调用父窗口jquery表达式不起作用。 But when i enable the alert call in closePopup function in the popup.php everything works fine. 但是,当我在popup.php的closePopup函数中启用警报调用时,一切正常。 - whats wrong with me? -我怎么了?

file test1.html 文件test1.html

<script type="text/javascript" src="jquerymin.js"></script>
<script type="text/javascript">
var visible_status=0;
function selectFn(sno){
if(sno==1){
    $('#id2').hide();
    $('#id1').show();
    visible_status=1;
}else if(sno==2){
    $('#id2').show();
    $('#id1').show();
    visible_status=2;
}
}

function popitup(url) {
    newwindow=window.open(url+'?parent_status='+visible_status,'name','top=300,width=400,height=250');
    if (window.focus) {newwindow.focus();}
        return false;
}
</script>
<select name='optionw' onchange="selectFn(this.options[this.selectedIndex].value);">
<option value="">Select</option>
<option value="1">Div1</option>
<option value="2">All</option>
</select>
<div id='id1' style="display:none;">DIV1</div>
<div id='id2' style="display:none;">DIV2</div>
<button onclick="popitup('popup.php');">popUp</button><br>`

my popup.php file 我的popup.php文件

    <script type="text/javascript" src="jquerymin.js"></script>
    <script type="text/javascript">
    var parent_status='<? echo $_GET[parent_status];?>';

    function closePopup() {
        window.opener.history.go(0);
        //alert('going to call parent selectFn('+parent_status+')');
        window.opener.selectFn(parent_status);
        self.close();
    }
    </script>

...Here  editing a part of the main page content...
<input type=button value=close_popup onclick="closePopup();">

after closing this popup i need , 关闭此弹出窗口后,我需要

  1. main page to be reloaded ( to show the edited content) 要重新加载的主页(以显示编辑的内容)
  2. to restore the div visible status as before clicking this popup. 与单击此弹出窗口之前一样,恢复div的可见状态。

You must make sure that the parent frame is finished loading before you try to execute some code that operates on the DOM (since the DOM might not be complete, yet). 在尝试执行在DOM上运行的某些代码之前,必须确保父框架已完成加载(因为DOM可能尚未完成)。

JS is synchonous in browsers but page loading isn't. JS在浏览器中是同步的,但页面加载不是。 The most simple way to implement this is to hook in body.onload() . 实现此目的的最简单方法是挂接body.onload() IIRC, jQuery offers a helper function for this. IIRC,jQuery为此提供了一个辅助功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM