How can I prevent closing a popup with the Escape key or browser back button?

0
Hello everyone,   I have a popup page, and when that particular popup appears, the user should not be able to close it using the Escape key or the browser's back button. Does anyone know how to implement this?   Thanks
asked
2 answers
3

Hello Priyanka:

 

I have had this requirement before and I resolved it with a normal page styling it as a pop up :)

 

**Option 1: Use a Normal Page Instead of a Popup

Mendix modals (popup pages) always allow ESC and outside-click to close. There’s no built-in Mendix setting to disable that.

Workaround: Instead of using a "popup" page, use a regular page with a modal-like design, and open it in content. This gives you full control:

  • No ESC key behavior

  • You can style it like a modal

💡 UI Tip

You can style the regular page with a transparent dark background and center the "modal box" using containers and CSS.

 

**Option 2: Block Escape with JavaScript

In the popup page, use a JavaScript snippet (via a widget like HTML/JS Snippet) to override Escape.

 

"document.addEventListener("keydown", function(event) {   

if (event.key === "Escape") {       

event.stopImmediatePropagation();       

event.preventDefault();   

}

}, true);"

 

This prevents the Escape key from doing anything on that page.

 

 You still need to prevent browser back navigation separately.  I have tried before this browser back button behavior to override it with Javascript action but it's risky and tricky, so I hardly not recommend to change the default behavior of the Dom since sometimes you can expect also some strange behaviors that you don't want to.

 

I hope it helps!

answered
1

Have you tried overriding the modal pop-up's close action?

Believe you can do this - 

add an action button to the pop-up that runs an empty nanoflow, and name it (say) 'NoUserCancel'

Then override the pop-up's close action to invoke that button 

Then hide the button using css.

 

If it works , its saves messing with javascript

 

 

answered