Show popup modal only once

I have a popup modat which will appear whenever a certain cookie doesn’t exists:

PHP

if (!isset($_COOKIE['mailinglist_popup']))

JS

<script>
	$(window).scroll(function () { 
		if($(document).scrollTop() > $(document).height()/2){
			$('#mailinglistModal').modal('show');
		} 
	});
	Cookies.set('mailinglist_popup', 'yes', { expires: 1000, path: '/' }); 
</script>

When I dismiss the modal with the close butt the modal It won’t popup anymore, however when I submit the form the success message replaces the form and when I close that, using the close button the success mesage keeps popping up. Only when I refresh the page it stops. What could be the reason, or evev better how can avoid it from popping up without refreshing the page?

Edit: I think I should have posted this in Javascript.

Whole lot of information missing here.

Why? What code is in the close button that prevents it?

Define ‘replaces the form’.

what success message? What’s governing that?

Even if the close button prevents the modal from being shown again, there will still be those (now unnecessary) .height() and .scrollTop() computations for each scroll event; so you’d best remove the scroll handler altogether after the modal got shown:

$(window).on('scroll', function handleScroll () {
  if ($(document).scrollTop() > $(document).height() / 2) {
    $('#mailinglistModal').modal('show')
    $(window).off('scroll', handleScroll)
  }
})
3 Likes

@m3g4p0p. Wow this is great. Works as a charm. :smile:

1 Like

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