简体   繁体   中英

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. But when i enable the alert call in closePopup function in the popup.php everything works fine. - whats wrong with me?

file 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

    <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.

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).

JS is synchonous in browsers but page loading isn't. The most simple way to implement this is to hook in body.onload() . IIRC, jQuery offers a helper function for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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