We will show newsletter popup when customers leave the site Instead of showing popup on customers visit the site:

  • When customer is going to close the website?
  • When customer is going to go to another website?

How to do this. Thanks.

  • Can you please add the code which you have tried for your popup? Commented Jul 26, 2017 at 9:23

2 Answers 2


This javascript 'onbeforeunload' event is used to detect browser close or changing website. You can use the event to display popup.

window.onbeforeunload = function () {
return "Do you really want to close?";
  • It doesn't work, indeed it displays the natif alert with the natif message but not with what your return, and is not what he is looking for ? HE WANT A NEWS LETTER POP UP !
    – PЯINCƎ
    Commented Jul 26, 2017 at 8:42
  • 1
    I think this answer provides a starting point. The OP can use this and in place of return he can add his popup code. @Prince Commented Jul 26, 2017 at 8:53
  • i want display popup newletter, not message
    – SonMai
    Commented Jul 26, 2017 at 9:07
  • 2
    The stay on the page message will appear but you can trigger to show the newsletter block in this function before return statement. And the user can decide to close the window or stay on page by seeing your newsletter window. Commented Jul 26, 2017 at 9:15
  • 2
    window.onbeforeunload = function () { jQuery('#newsletter').toggle(); return true; }; Commented Jul 26, 2017 at 9:17

You can do it in jquery. Please refer the Mouseleave and Mouseout function and set the event in your body tag. Popup block code shoud be in your header.phtml

For Example :

$( "body" ).mouseout(function() {
  //call your popup block



Hope it will usefull for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.