views:

233

answers:

1

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.
+1  A: 

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.

Aaron Digulla
@Aaron Digulla where i have to use body.onload() fn ?
Kumar
That's the tricky part: You must install it in the body of the parent frame ... which hasn't been loaded, yet. To make this work, try to put the item to be selected into the URL of the top frame (i.e. get the url from the history and attach "?select=..." and process this new parameter during onload() of the top frame.
Aaron Digulla
I cant change the parent url, bcoz, it will reload the main page and put the view at the top of the page.(the main page is long one. so, i need to restore the previous position after closing the popup, but with updated values.)i am new to jquery - i cant able to findout the required approach.help please.thanks in advance.
Kumar
You must save the value somewhere. You can save it in the session on the server or in the URL of the new page. But you can't simply go up, and call some javascript which is going to be killed by the loading the page. Consider using an iframe somewhere. Or just replace part of the main page with an AJAX request.
Aaron Digulla