Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omurphy27/391d2d58eeb7a4603108 to your computer and use it in GitHub Desktop.
Save omurphy27/391d2d58eeb7a4603108 to your computer and use it in GitHub Desktop.
Show bootstrap modal window only once onload via JS Cookie.js
@rey17k
Copy link

rey17k commented Nov 13, 2017

sir why its still opening every time i open the same page.

@YK7479
Copy link

YK7479 commented Jan 5, 2018

When I was run this code showing directly instead of a modal window.

@YK7479
Copy link

YK7479 commented Jan 5, 2018

=========== working now with the current date and time to removing cookies ===========

<script type='text/javascript'> function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } $(function(){ if(getCookie("lastAccessed") != ""){ var temp = new Date(getCookie("lastAccessed")); var noOfHrs = (new Date().getTime()-temp.getTime())/(1000*60*60); if(noOfHrs > 24){ showGreetings(); } }else{ showGreetings(); } document.cookie = "lastAccessed= "+new Date(); }); function showGreetings(){ var overlay = $('
'); overlay.show(); overlay.appendTo(document.body); $('.popup').show(); $('.close').click(function(){ $('.popup').hide(); overlay.appendTo(document.body).remove(); return false; }); $('.x').click(function(){ $('.popup').hide(); overlay.appendTo(document.body).remove(); return false; }); } </script>

@razinbunsu
Copy link

This will fix the modal to show only once
if ( !Cookies.get('popup') ) { setTimeout( function() { $('#siteModal').modal().on('shown.bs.modal', function() { Cookies.set('popup', 'true', { expires: 3, path: "/" }); }); }, 600); }

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