This is immoral, and if you were my son and I caught you trying to do this kind of thing on a website you’d be out of my will.
Anyway... you mentioned in a comment in the other reply a certain site. What the site does is not open a popup, but a modal. This is different from opening a popup because it does not interfere with the behavior of the browser and does not open a new tab.
The site uses a library called Ouibounce, that you can find on Github. What the library code does is a function that checks the Y coordinate of the mouse cursor (which for Javascript is relative to the window). If the mouse is less than a number of pixels from the top of the window, it opens a modal.
Note that this means that the modal opens if you move the mouse to any higher point of the browser, like other tabs or even a menu. It will not necessarily fire only when someone moves the mouse to close or change tab, nor will it open the modal if the user uses keyboard shortcuts. It will also not work in mobile browsers or using different user experiences for obvious reasons.
If you have no soul or ethics you can modify the Ouibounce code to open a popup even instead of a modal.
I do not understand why it is immoral, I see only a page offering discount, nothing else, explains me the immoral part
– Fabio
@Fabio popups are unpleasant and can be vectors of attacks. Opening popups to make offers and advertisements is unethical. This is why today browsers block certain types of popup by default. This also causes, for example, the need for you to have to manually authorize popups on certain websites that have made bad design decisions. Note that the site you indicated in the other reply’s comment does not open popups. The library it uses opens modals, are something completely harmless (if not in aesthetics, at least in safety).
– Oralista de Sistemas