How to Reload Page When Default ❌ Close Icon is Clicked in GlideModal?

akanksha99
Tera Contributor

I’m using a GlideModal in my UI script to show a popup with some inline HTML content (using renderWithContent). Here's the code:

function showModal(cTitle, htmlContent) {
var gm = new GlideModal();
gm.setTitle(cTitle);
gm.setPreference('focustrap', true);
gm.renderWithContent(htmlContent);
}

I want the page to reload automatically when the user clicks the default (close icon) on the modal.

I tried this, but it doesn’t work:

gm.onClose = function() {
location.reload();
};

Since I’m not using a UI Page or custom modal, just renderWithContent, how can I detect that the button was clicked and reload the page in that case?

Any ideas or solutions would be appreciated.

Thanks!

2 REPLIES 2

nileshwahul
Kilo Sage

Hi @akanksha99 ,
Not sure if you are still in need for this solution. But I wanted the same and could not find any. 
So I am putting work around solution I did here.

I have overwrite the OOB logic for the ServiceNow's GlideModal Destroy function, but only for the one which we want to reload.

 

You just need to add below function in your UI page client script section at end.

// Runs as soon as the UI Page loads
addLoadEvent(function () {
  var modal = GlideModal.get();
  if (modal) {
    // We override the destroy function of this specific modal instance
    var originalDestroy = modal.destroy;

    modal.destroy = function () {
      // 1. Call the original ServiceNow logic so it actually closes
      originalDestroy.apply(this);

      // 2. Trigger your reload
      window.parent.location.reload();
    };
  }

 

---------------------------------------------------------------------------------------------------

Please mark my answer as helpful/correct if it resolves your query.

Thanks,
Nilesh Wahule

LinkedINlinkedin.com/in/nilesh-wahule/

---------------------------------------------------------------------------------------------------

 

pururavasin
Tera Contributor

Hello,

Please check following snippet, if any of it works:

 

location.reload();

Or

top.location.reload();