Close javascript-popup-window from anywhere

后端 未结 3 1141
广开言路
广开言路 2021-01-28 21:43

I\'m trying to build a popup that can be closed from anywhere.

On the Mainpage you have the option to open it. At any point while browsing the mainpage the user shall be

3条回答
  •  有刺的猬
    2021-01-28 22:11

    There are three solutions I propose depending on what the problem actually is.

    1) closepopup() does not work on other pages

    If you need to close the popup from page B when it was opened from page A the following code will allow you to obtain a reference to the popup from page B and close it. Reference: Find window previously opened by window.open

    PageA.html

    
    
    Open window...
    Go to Page B

    PageB.html

    
    
    
    
    
    Close window...

    2) closepopup() does not work on the same page as window is opened

    A global reference to my_window is needed so you would need to change the code like this:

         var my_window; //global reference here
        function popuponclick()
           {
              my_window = window.open("", "mywindow","status=1,width=350,height=150");
           }
    
           function closepopup()
           {
              my_window.close ();
           }
    

    3) Final third solution using frames

    You separate your page into 1 frames (one which is 100%) and another that is 0%.

    You add the window open/close code in the parent frame. Than you call the window control code via cross frame javascript commands.

    Frames HTML Containing Javascript

    
    
    My example
    
    
    
    
    
    
    
    
    
    

    Internal Frame (frame.html) - calling parent Javascript

    
    
     Open Window 
    Close Window
    Refresh Frame (and try again)

提交回复
热议问题