Sankar M
Sankar M

Reputation: 4809

How to close a popup window in a parent window?

I need to close a popup window which has been loaded by a parent window.

This popup window is a Documentviewer window in my webapp.

I need to close this viewer by clicking a logout button which is in master page.

My code:

public string MySession //server side code

{
    get 
    {
        if (Session["RegID"] != null)
        {
            return Session["RegID"].ToString();
        }
        else
        {
            return "";
        }
    }

}


//client side code
   $(window).load(function() {

      Start();
   });


    function Start()
    {
      timedCount();
      var t=setTimeout("Start()",10000);
    }


   function timedCount()
   { 
     /*var out="<%=Session["RegID"]%>";*/


      var out='<%=MySession%>'; 
      if(out!="")
      {
        alert(out);       
      }else
      {
        window.close();
      }

   }

Server code is executed at very first time only.

My target is to close the popup if it is opened when user logs out.

Upvotes: 0

Views: 7340

Answers (2)

Mehmet Osmanoglu
Mehmet Osmanoglu

Reputation: 1212

Put your popup window in global variable:

<script>
    var popupWindow;

    function openw(url) {
        popupWindow = window.open(url, "popup", "");
    }

    function closew() {
        if (popupWindow) {
            popupWindow.close();
        }
    }

</script>
<a href="javascript:openw('about:blank')">open</a><br />
<a href="javascript:closew()">close</a>

Upvotes: 0

pimvdb
pimvdb

Reputation: 154958

You probably have something like this on your parent page:

window.open(...);

If you change this to:

var popup = window.open(...);

then at any time you can close it by coding:

popup.close();

http://jsfiddle.net/pimvdb/bjkNx/1/

Upvotes: 2

Related Questions