Hi i want to close my popup window..
how to do it?
window.close() is not working..

i m showing my popup using jquery..
i want to close popup when close button is clicked..
Regards..
fahrad

Dani AI

Generated

Short summary: the popup you show with jQuery/bPopup is a DOM overlay, not a browser window, so calling window.close() won’t close it. window.close() only reliably closes windows/tabs opened by script (not an in-page div). (developer.mozilla.org)

Because you’re using bPopup (opened from the combobox change), use the plugin’s own close mechanism. bPopup recognizes a close-button class (the plugin defaults to bClose / b-close) and also exposes a programmatic API (the instance returned by .bPopup() has a close() method). Adding a close button inside the popup or calling the close() method will properly remove the overlay. (github.com)

Minimal pattern you can adapt:

<div id="myPopup" style="display:none">
  <button class="popup-close">Close</button>
  <div class="content">...</div>
</div>
var popupInstance;

function openPopup() {
  popupInstance = $('#myPopup').bPopup(); // store instance so you can call .close()
}

$(document).on('click', '.popup-close', function(e){
  e.preventDefault();
  if (popupInstance && typeof popupInstance.close === 'function') {
    popupInstance.close();
    popupInstance = null;
  } else {
    // fallback if needed
    $('#myPopup').hide();
  }
});

Use delegated handlers (.on(...)) for dynamic elements rather than older .live()/.bind() patterns if you upgrade jQuery. Also ensure the bPopup script is loaded after jQuery and that the popup HTML includes the close button (or the bClose class) so the plugin can hook it. (jquery.com)

If the popup contains an iframe and you need to close it from inside that iframe, be aware of opener/same-origin restrictions: window.opener can be null or limited for cross-origin frames, so you may need postMessage or a parent function on the same origin to trigger the close. (developer.mozilla.org)

Notes tied to the thread: — switch from window.close() to the bPopup close API or add a bClose button; and — jQuery UI is a valid alternate approach, but with bPopup the plugin API shown above is the simplest fix.

Recommended Answers

All 4 Replies

How are you oppening the popup?

Well if you are using jQuery might I reccomend using the dialog feature.

Setting it up is simple

1.) create a div in your html and a button so the dialog can be opened and closed :

//within html

<div id='my_first_dialog'>  <img src='dancing_homer.gif'/> </div> 
 <div id='diag_button'>  CLICK ME!!!!!! </div>

2.) You want to create an event listener so your browser knows to activate the event when the button is open

<script>
 var open = 'no' 
  $('#my_first_dialog').dialog(); // hides your div and gives it the dialog attributes 
  //add in a listener so it knows when  to open your dialog 

  $('#diag_button').live('click',function(){
      if(open == 'no'){
         $('#my_first_dialog').dialog('open'); //opens your dialog
         open = 'yes'; 
      } 
      else{
         $('#my_first_dialog').dialog('close'); //closes your dialog
       }

   });
</script>

Now if you so choose you can get more technical, and add buttons to your dialog which could also do some action. But ill leave that up to you to look up.

you can find it here at : http://jqueryui.com/dialog/

Hope this helps! :)

That is if what your doing isnt an extra window.... to another webpage. In that case most of todays browsers put these into tabs and add the needed close and minmize buttons for the users to use anyway...

@AleMonterio

I m calling popup from combobox binding

$('#cbmAppIniId').bind('change', function(e) { 
                e.preventDefault();

                var value = $(this).val();

                if(value == 'ShortList')
                {

                   $('#element_to_pop_up').bPopup();

                   }

i tried my best but pop up is not clossing..
:(

Farhad, I don't know bPopUp, so I can't help you.

But you should try the suggetion made by soapyillusion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.