Skip to content

Instantly share code, notes, and snippets.

@niranyousuf
Last active April 6, 2023 08:05
Show Gist options
  • Save niranyousuf/7a1b9022424ab129d63d3596647455b2 to your computer and use it in GitHub Desktop.
Save niranyousuf/7a1b9022424ab129d63d3596647455b2 to your computer and use it in GitHub Desktop.
If any one want to leave from browser you can show him pop up
if (document.querySelector('.en-popup')) {
var popUp = document.querySelector('.en-popup');
document.addEventListener("mouseout", function popUpEventHandler(e) {
if (e.toElement === null) {
popUp.classList.add('show')
document.body.classList.add('popup-no-scroll')
this.removeEventListener('mouseout', popUpEventHandler);
}
})
var closePopUp = document.querySelectorAll('.close-popup');
closePopUp.forEach((closePop) => {
closePop.addEventListener('click', () => {
popUp.classList.remove('show-en-popup')
document.body.classList.remove('popup-no-scroll')
})
})
}
@niranyousuf
Copy link
Author

`(function ($) {
"use strict";

    var popUp = $('.en-popup');
    var popUpTriggered = false;

    if ($(window).width() > 992) {
        $(document).on("mouseleave", function () {
            if(popUpTriggered) return;
            popUp.addClass('show')
            $(document.body).addClass('popup-no-scroll')
            popUpTriggered = true;
        })
    }

    if ($(window).width() < 992) {
        $(window).one('beforeunload', function popUpEventHandler(e) {
            popUp.addClass('show')
            $(document.body).addClass('popup-no-scroll')
            e.preventDefault()
            setTimeout(() => {
                window.stop();
            }, 1000);
        })
    }

    $('.close-popup').on('click', () => {
        popUp.removeClass('show-en-popup')
        $(document.body).removeClass('popup-no-scroll')
    })

})(jQuery);`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment