Close spModal from an onLoad catalog client script for a record producer

Lima Beauvais
Giga Guru

Good afternoon,

I used the script below within an onLoad catalog client script for a record producer to open a modal window. I want to close the popup window when the user selects the browser back button. I tried modalInstance.dismiss(); but it is not working. I would appreciate your input for a solution. Thanks!

 

var returnURL = "some URL";
var modalInstance = spModal.open({
backdrop: true,
keyboard: false,
noDismiss: true,
message: txtContent,
title: 'Popup Title',
size: 'lg',
buttons: [{
label: 'I Agree',
primary: true
},
{
label: 'I Disagree',
cancel: true
}
]

}).then(function() {
// user agrees
}, function() {
top.window.location.replace(returnURL);
}, function() {
spModal.open({
widget: 'widget-id'
});

});

1 ACCEPTED SOLUTION

I found a workaround for now. It is ugly. I just reload the window when the user selects the back button to get rid of the spModal. I was able to fix the addListesner issue by preceding window with top. I will keep digging until I can close the spModal but for now the users have to live with the workaround. Here's the working code:

top.window.addEventListener('popstate', function(event) {
    if (modalInstance) {
        top.window.location.reload();
    }
});


View solution in original post

7 REPLIES 7

I found a workaround for now. It is ugly. I just reload the window when the user selects the back button to get rid of the spModal. I was able to fix the addListesner issue by preceding window with top. I will keep digging until I can close the spModal but for now the users have to live with the workaround. Here's the working code:

top.window.addEventListener('popstate', function(event) {
    if (modalInstance) {
        top.window.location.reload();
    }
});


Jim Coyne
Kilo Patron

What is the spModal for?  What's the use case?  Using the back button is definitely NOT a great idea.

 

Can you switch things around to use variables right at the top of the RP for the users to answer before anything else is shown?

Hello Jim,

The spModal is to display several paragraphs of acknowledgement text for the user to agree before continuing. The spModal is working fine. But if the user clicks the back button before acknowledging, it returns the user back to the previous page and the modal still stays on the screen.