How to prevent user from leaving page


I would like to prevent a user from navigating away from a page. The browser should prompt the user with a confirmation dialog when they try to leave the page.

Steps to reproduce

Code snippet:

        window.addEventListener('beforeunload', function (e) {
            e.returnValue = '';
""", unsafe_allow_html=True)

Expected behavior:

The broser displays a confirmation dialog before leaving

Actual behavior:

Nothing happens

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: Streamlit, version 1.17.0
  • Using PipEnv
  • OS version: MacOS
  • Browser version: Version 115.0.5790.170 (Official Build) (arm64)

I also hope to have such a feature.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.