Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does any one know a better way to get the handle of an opened popup window?

Problem: Need to get a handle of an already opened javascript popup window (handle = window.open(…)) from its opener window, multiple requests later after its opener (parent) window has been refreshed and javascript variables reset.

For example, parent window could have javascript as follows:

<script type="text/javascript">
      var popupHandle;
      function openPopUp() {
            popupHandle=window.open('http://www.google.ca', 'popupTest');
            popupHandle.focus();
            return popupHandle;
      }
      // To get handle, need to reopen popup with same name as original (popupTest).
      function getPopUpHandle() {
            return openPopUp();
      }
      // If getting handle to close, open as small as possible and close so it’s not too noticeable.
      function closePopUp() {
            popupHandle=window.open('', 'popupTest', 'directories=no,location=no,menubar=no,status=no,resizable=no,scrollbars=no,titlebar=no,top=1,left=1,width=1,height=1');
            popupHandle.close();
      }
</script>

If you know of a better solution, please let me know.

How I am using it in my app: I have a list of images displayed on one side of the screen. On the other side I have a form that allows me to submit information based on the image. When the form is submitted, it attaches the image referenced.

When an image is clicked on, it opens in a popup.

When the form is submitted, the next image in the list should open in the popup. The popup could have been closed by the user.

like image 751
CalvinTreg Avatar asked Nov 15 '22 14:11

CalvinTreg


1 Answers

It's a bit of a kludge, but you could put a script in the opened window that attempts to call a function on its window.opener to inform the parent window of its existence.

So, the image-display window would have a function like

setInterval(function(){ 
   if(window.opener.registerChildWindow){ 
        window.opener.registerChildWindow(window);
   } 
 }), 100);

This would attempt call the registerChildWindow function on the parent window 10 times a second, as long as that function exists, no matter what page the parent window was on. I'm not entirely sure how to get a window handle out of an existing window (passing window was a guess), but that should at least be something to play around with.

like image 123
Luke Rinard Avatar answered Dec 22 '22 12:12

Luke Rinard