3

How to close opened popup when it opens based on condition.

It looks very strange but I have requirement that based on some condition, if it becomes true popup should automatically close, How to do that?

I had return .dialog("close") on document.ready event but it is not working. Any idea?

UPDATE

I found problem is there actually popup is close but again after completing ready event it is opened again. Any help?

commit
  • 4,777
  • 15
  • 43
  • 70

7 Answers7

5

One of the simple solutions could be

$('#dialog-close-btn').trigger('click') 

where you point to dialog close button.

SharpC
  • 6,974
  • 4
  • 45
  • 40
Aivus
  • 502
  • 5
  • 15
3

If it is a bootstrap modal, you can use the below code to show / hide the popup.

$('#modal').modal('toggle');  //To show and hide

if(condition) {
    $('#modal').modal('hide');  //To hide
}

$('#modal').modal('show');  //To show
AlexB
  • 7,302
  • 12
  • 56
  • 74
2

This is an old post, but incase someone else runs into this issue, I had a similar problem, with a jquery popup that wouldn't close even with

$("#popup").dialog('close');

The solution, adapted from a workmate's code, was to have a control variable on top of the script section and set it to 1 when I open the popup, and reset it to 0 when I want it to close. Additionally, I remove the popup from the html on pageLoad (still in javascript) like this:

<script>
<code>
  var $l = jQuery.noConflict();
  var cntDialog = 0;
</code>

function pageLoad(sender,args) {
  $l(function () {
    if (cntDialog == 0) {
      RemoveDialog();
    });
  }

function initializeDialog(){
  $l("#popup").dialog({
    ...
    close: (function (sender,event) {
    RemoveDialog();
  })
});}

function RemoveDialog() {
  cntDialog = 0;
  $l("#popup").remove();
}
</script>

Hope it helps

Keith Harrison
  • 1,567
  • 14
  • 22
Ricardo Appleton
  • 679
  • 10
  • 22
0
if (condition){    
   $('dialog id').dialog('close');    
}
Beniamin Szabo
  • 1,909
  • 4
  • 17
  • 26
0

This should do the job in the popup window:

<script>
    $(function() {
        if (condition) {
            window.close();
        }
    });
</script>
Thierry J.
  • 2,148
  • 16
  • 23
0

You have to add some tag in server side script and you have to set event listener on client side for close the popup close.

Means you have to set something like:

parent.postMessage("CLOSE","*");  

on server.

and set listener client side like:

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

// Listen to message from child window

 eventer(messageEvent,function(e) {
    var key = e.message ? "message" : "data";
        var data = e[key];
    },false);

I don't have time to check this. Hope this help you.

Or i go through following URL:

https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

http://nyamsprod.com/blog/2012/introduction-to-window-postmessage/

http://benalman.com/code/projects/jquery-postmessage/examples/iframe/

My given code is from URL:

Html5 - Cross Browser Iframe postmessage - child to parent?

You can add dilogue close code inside listener.

Thanks

Community
  • 1
  • 1
Naitik
  • 1,455
  • 15
  • 26
0

$('.my-modal').hide() will close that active modal.